请选择 进入手机版 | 继续访问电脑版

技术控

    今日:5| 主题:53736
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Android Push Notification Using Firebase Cloud Messaging (FCM)

[复制链接]
女王范 发表于 2016-10-8 22:10:54
295 3

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
In this tutorial you will learn to implement android push notification using firebase cloud messaging (FCM).
  Firebase is a real time cross platform database that provides various functionalities like authentication, storage, notification, etc. Firebase is becoming popular among developers due to its simplicity and easy implementation. In earlier days Google Cloud Messaging (GCM) was used to implement push notification. But Firebase Cloud Messaging (FCM) is better and easy to implement than GCM.
   

Android Push Notification Using Firebase Cloud Messaging (FCM)

Android Push Notification Using Firebase Cloud Messaging (FCM)

   Android Push Notification Using Firebase Cloud Messaging (FCM)

   Prerequisites

  To use FCM there are few minimum requirements.
  
       
  • Device with Android 2.3 or newer and Google Play services 9.6.0 or newer   
  • The Google Repository from the Android SDK Manager   
  • Android Studio 1.5 or higher version.  
   Android Project

   Create a new android studio project with package name com.pushnotificationexample and copy the package name.
   Firebase Project

   Go to Firebase console https://firebase.google.com/console/ to create a new project.
   Click on Create New Project button. Enter some project name and your country. Click on Create Project button to create the firebase project.
   

Android Push Notification Using Firebase Cloud Messaging (FCM)

Android Push Notification Using Firebase Cloud Messaging (FCM)
  
   Choose Add Firebase to your Android app option.
   

Android Push Notification Using Firebase Cloud Messaging (FCM)

Android Push Notification Using Firebase Cloud Messaging (FCM)
  
   Enter the package name that you copied earlier and click Add App button. You will get a google-services.json file.
   

Android Push Notification Using Firebase Cloud Messaging (FCM)

Android Push Notification Using Firebase Cloud Messaging (FCM)
  
   Add Firebase to Android Project

   Select Project view in Android Studio and paste the google-services.json file under app folder.
   

Android Push Notification Using Firebase Cloud Messaging (FCM)

Android Push Notification Using Firebase Cloud Messaging (FCM)
  
  Open project level build.gradle file and add following line under dependencies.
  1. classpath 'com.google.gms:google-services:3.0.0'
复制代码
Open app level build.gradle file and add following line under dependencies.
  1. compile 'com.google.firebase:firebase-core:9.6.0'
复制代码
Also add following line at the end of app level build.gradle file.
  1. applyplugin: 'com.google.gms.google-services'
复制代码
Firebase Cloud Messaging (FCM) Implementation

   Create a new class MyFirebaseMessagingService that extends FirebaseMessagingService . Add following code inside it.
  1. package com.firebasepushnotification;
  2. import android.app.NotificationManager;
  3. import android.app.PendingIntent;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.media.RingtoneManager;
  7. import android.net.Uri;
  8. import android.support.v4.app.NotificationCompat;
  9. import com.google.firebase.messaging.FirebaseMessagingService;
  10. import com.google.firebase.messaging.RemoteMessage;
  11. public class MyFirebaseMessagingService extends FirebaseMessagingService {
  12.     @Override
  13.     public void onMessageReceived(RemoteMessageremoteMessage) {
  14.         //calling method to generate push notification
  15.         sendNotification(remoteMessage.getNotification().getBody());
  16.     }
  17.     private void sendNotification(String messageBody) {
  18.         Intentintent = new Intent(this, MainActivity.class);
  19.         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  20.         PendingIntentpendingIntent = PendingIntent.getActivity(this, 0, intent,
  21.                 PendingIntent.FLAG_ONE_SHOT);
  22.         UridefaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  23.         NotificationCompat.BuildernotificationBuilder = new NotificationCompat.Builder(this)
  24.                 .setSmallIcon(R.mipmap.ic_launcher)
  25.                 .setContentTitle("Push Notification Example")
  26.                 .setContentText(messageBody)
  27.                 .setAutoCancel(true)
  28.                 .setSound(defaultSoundUri)
  29.                 .setContentIntent(pendingIntent);
  30.         NotificationManagernotificationManager =
  31.                 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  32.         notificationManager.notify(0, notificationBuilder.build());
  33.     }
  34. }
复制代码
  Now we have to add above service in AndroidManifest.xml . So add following code anywhere between <application> <application/> tag.
  1. <service
  2.     android:name=".MyFirebaseMessagingService">
  3.     <intent-filter>
  4.         <actionandroid:name="com.google.firebase.MESSAGING_EVENT"/>
  5.     </intent-filter>
  6. </service>
复制代码
Build and run your app.
   Sending Push Notification

   Go back to Firebase Console and select Notifications option from left menu. Click on New Message button.
   Add a message in Message text box. Choose Target as User Segment . This will send the notification to all the users using this android app. You can also send notification to a particular user’s device.
   Choose the app package name from drop down menu. Finally click on Send Message button to send the message.
   

Android Push Notification Using Firebase Cloud Messaging (FCM)

Android Push Notification Using Firebase Cloud Messaging (FCM)

   If you have done everything correctly then you will see a push notification in your device.

Android Push Notification Using Firebase Cloud Messaging (FCM)

Android Push Notification Using Firebase Cloud Messaging (FCM)

  Comment below if you are facing any problem in above android push notification tutorial.



上一篇:Golang中一个不错的处理 JSON 的库 go-dproxy
下一篇:HTTP/2 基本帧说明
雁玉 发表于 2016-10-9 02:17:11
非常好,顶一下
回复 支持 反对

使用道具 举报

征途鸟 发表于 2016-10-16 12:18:13
小手一抖,经验到手,为了升级,到处游走,纯属路过,不要介意,高抬贵手,立马飘走.
回复 支持 反对

使用道具 举报

dorozhtDex 发表于 2016-11-14 15:09:44
边撸边过
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )

© 2001-2017 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表