Firebase Cloud Messaging接入文档
来源:互联网 发布:光纤网络监控方案 编辑:程序博客网 时间:2024/05/20 15:36
一、Firebase Cloud Messaging简介
参考文档:https://firebase.google.com/docs/cloud-messaging/
1、引入
说起Firebase Cloud Messaging(简称FCM),可能多数人都不知道,但是说起GCM(Google Cloud Messaging)想必安卓开发人员都了解,虽然目前国内的安卓设备都阉割了谷歌服务导致GCM并不能正常使用,但是GCM的知名度还是有的。
2、工作原理
即使关闭了苹果手机的软件,手机依然可以收到软件推送的消息,原因就是该消息是从软件的服务器经由苹果的服务器转发到苹果手机上的,而GCM的作用跟该苹果的推送原理类似,软件服务器将消息交给谷歌服务器,谷歌的服务器将消息推送到安卓设备上,可以极大的减少无用软件的唤醒,降低手机耗电量。
3、现状
由于谷歌在国内被屏蔽访问,国内的大部分手机都阉割了谷歌服务,这就导致了GCM不可用,各大厂商都想方设法保持自己的软件在安卓设备上处于活动状态,便于及时将各种消息推送到安卓设备上,导致人们对安卓的印象就是卡顿和耗电,这跟GCM的不可用有极大关系。
由于公司业务需要,要做国外运营的软件,所以就需要接入GCM,上谷歌查询文档,发现官方已经不再推荐开发者使用GCM了,而推荐更为强大的FCM,可以认为FCM是GCM的改进版或者加强版。
二、接入准备工作
1、开发设备
装有谷歌服务器的安卓手机一台
2、开发环境
Android Studio
3、控制台设置
(1)、创建应用
打开Firebase控制台:https://console.firebase.google.com/
点击新建项目
(2)、填写项目名称
在弹出的窗口填写项目名称,然后点击创建项目
(3)、集成Firebase到安卓
在Overview
选项中点击将Firebase添加到您的Android应用
(4)、填写包名和sha1
在弹出的窗口中输入项目的包名
和sha1
,然后点击添加应用
(5)、下载google-services.json
创建成功后点击Download google-services.json
,然后将项目切换到Project视图,将下载的文件放入到app的根目录
PS:切记,添加的sha1如果是debug版,那么正式版的sha1也需要添加进去。点击Overview后面的设置按钮,再选择项目设置,找到常规选项卡,在下方继续添加sha1即可,保存后记得重新下载google-services.json并放到app的根目录中
三、Android端集成FCM
参考文档:https://firebase.google.com/docs/cloud-messaging/android/client
1、修改gradle文件
(1)、项目级build.gradle
在项目级的build.gradle文件中的dependencies节点中添加下面代码
classpath 'com.google.gms:google-services:3.0.0'
(2)、应用级build.gradle
在应用级的build.gradle文件中添加依赖
compile 'com.google.firebase:firebase-messaging:10.0.2'
将下面的代码放到文件的最后一行
apply plugin: 'com.google.gms.google-services'
2、创建MyFirebaseInstanceIDService
在项目中新建MyFirebaseInstanceIDService,继承FirebaseInstanceIdService,并重写下面的方法
@Overridepublic void onTokenRefresh() { super.onTokenRefresh(); String refreshedToken = FirebaseInstanceId.getInstance().getToken(); Log.d(TAG, "Refreshed token: " + refreshedToken);}
在清单文件中注册Service
<service android:name=".MyFirebaseInstanceIDService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter></service>
3、MyFirebaseMessagingService
新建MyFirebaseMessagingService,继承FirebaseMessagingService,重写下面方法
@Overridepublic void onMessageReceived(RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); Log.d(TAG, "From: " + remoteMessage.getFrom()); // Check if message contains a data payload. if (remoteMessage.getData().size() > 0) { Log.d(TAG, "Message data payload: " + remoteMessage.getData()); } // Check if message contains a notification payload. if (remoteMessage.getNotification() != null) { Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); }}
在清单文件中注册
<service android:name=".MyFirebaseMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter></service>
4、获取token
在项目中,可以通过下面代码获取设备的token,并将token发送到服务器,便于服务器进行指定设备的消息推送
FirebaseInstanceId.getInstance().getToken();
5、设置图标和背景色
在清单文件中添加下面代码,可以分别设置通知的图标和图标的背景色
<meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/icon_logo" /><meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/colorPrimary" />
四、发送消息
1、控制台发送消息
在控制台左侧选择Notifications,然后点击写新消息,就可以向指定设备或全部设备发送消息了
2、通过发送post请求发送消息
(1)、请求地址
请求的地址为:https://fcm.googleapis.com/fcm/send
(2)、请求头部
http头部必须包含两部分
Content-Type:application/json
Authorization:key=YOUR_SERVER_KEY
其中serverkey可以在控制台的设置–项目设置–云消息传递–服务器密钥中获取
(3)、发送内容
如果发送通知消息,则发送的内容类似与下面的代码
{ "notification": { "title": "Portugal vs. Denmark", "text": "5 to 1" }, "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."}
其中to的值是设备的token
更多参数设置可以参考:https://firebase.google.com/docs/cloud-messaging/concept-options?hl=zh-cn#notifications
- Firebase Cloud Messaging接入文档
- FCM(Firebase Cloud Messaging) 接入问题说明
- Google FCM推送(Firebase Cloud Messaging)
- 整合Firebase Cloud Messaging (FCM)到Eclipse工程
- Android一点 让eclipse也支持FCM(Firebase Cloud Messaging)
- GCM 的升级版——FCM(firebase cloud messaging)
- Firebase系列之---Cloud Messaging/Notifications(云消息,推送)的使用
- Google Cloud Messaging
- FCM issues (Firebase Cloud Message)
- Google Cloud Messaging for Android
- Google Cloud Messaging for Android
- android: Google Cloud Messaging (GCM)
- google cloud messaging(GCM)机制.
- Android Cloud to Device Messaging Framework
- Google Cloud Messaging Android版开发出现
- Android Cloud to Device Messaging Framework
- Google Cloud Messaging(GCM) for Android
- Google Cloud Messaging(GCM Google云推送)
- 这是我的第一个博客,记录一下日期
- SQL表变量(declare)+公用表(with as)格式
- 《ACM程序设计》书中题目 W
- 多线程编程
- 欢迎使用CSDN-markdown编辑器
- Firebase Cloud Messaging接入文档
- 新闻案例本地版(listview实现)
- 反射小例and可变参数
- 多线程编程实例
- 设计模式之策略模式
- Android中设置图片的圆形遮罩的几种方式
- 事件处理(Handling Events)和委托(Delegate)代码示例(一)【UE4】【C++】
- php和python请求socket服务器
- PHP的用法