Android端
先决条件
一台运行 Android 2.3 (Gingerbread) 或更新版本并运行 Google Play 服务 9.6.1 或更新版本的设备。
SDK Manager需要下载Google play Services SDK
Android Studio 1.5 或更高版本
-
Android Studio 项目及其捆绑包名称。
2.2 之前的 Android Studio 版本中的 Instant Run 与 Firebase Analytics 不兼容,并且会阻止其收集某些事件。建议禁用 Instant Run 或升级到 Android Studio 2.2 +
将Firebase添加至Android项目
- 首先,要在Firebase console中创建一个Firebase项目。如果已经有一个与您的移动应用关联的现有 Google 项目,请点击 Import Google Project。 否则,请点击 Create New Project。
- 点击 Add Firebase to your Android app 并按设置步骤进行操作。如果在导入现有 Google 项目,这可能是自动进行的,您只需下载配置文件即可。
- 按照提示输入应用的包名称。
包名称很重要,不要填错
。 - 最后,下载google-services.json 文件,然后copy到app目录下
添加SDK
首先在根级别的build.gradle
文件添加一条规则。以包含Google服务插件
// ...
dependencies {
// ...
classpath 'com.google.gms:google-services:3.0.0'
}
然后在模块Gradle文件中,底部
添加apply plugin
行,以启用 Gradle 插件:
apply plugin: 'com.android.application'
android {
// ...
}
dependencies {
// ...
compile 'com.google.firebase:firebase-core:9.6.1'
}
// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'
然后根据需求添加Firebase SDK依赖项。上面的com.google.firebase:firebase-core:9.6.1
。
上面的依赖firebase如果是从9.6.1升级到10.0.1,gcm也需要升级到10.0.1,否则会出现crash。
Firebase功能库完整列表:
Gradle 依赖项行 | 服务 |
---|---|
com.google.firebase:firebase-core:9.6.1 | Analytics |
com.google.firebase:firebase-messaging:9.6.1 | Cloud Messaging / Notifications |
... | ... |
在Android studio 2.2+,上面的步骤可以用Tools->Firebase,然后根据提示实现集成。
设置FCM客户端
- 自定义
MyFirebaseMessagingService
继承FirebaseMessagingService
,重写onMessageReceived
方法接收通知消息弹通知栏,FCM有两种消息,data message和 notification message,notification只有在后台的时候才会走这个方法,data message不管在后台还是前台都会走这个方法。 - 自定义
MyFirebaseInstanceIDService
集成FirebaseInstanceIdService
用户token的创建,转换和更新,在onTokenRefresh()
方法中获取token并上传到服务器。 - 获取token:
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(refreshedToken);
- 在AndroidManifest文件中配置:
<!-- [START firebase_service] -->
<service
android:name="packagename.MyFirebaseMessagingService"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<!-- [END firebase_service] -->
<!-- [START firebase_iid_service] -->
<service
android:name="packageName.MyFirebaseInstanceIDService"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<!-- [END firebase_iid_service] -->
Server端
Server端实现步骤如下:
FCM有三种消息类型,分别为Notification message
,Data Message
,Messages with both notification and data payload
-
Notification Message 通知消息,当App在前台的时候会走到我们自定义的
MyFirebaseMessagingService
中的onMessageReceived
方法,当在后台的时候由系统弹通知栏,当app被杀死的时候,从Firebase后台发送是收不到的。对应的HTTP POST请求:
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key= App Key
{
"notification" : {
"body" : "You have a new message",
"title" : "",
"icon" : "myicon" // Here you can put your app icon name
},
"to" : "token..."
}
-
Data Message 只有当App在前台或者在后台的时候,才会走到
MyFirebaseMessagingService
中的onMessageReceived
方法,当App被杀死当情况下会不会走到这里,需要测试。HTTP POST请求:
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key= App Key
{
"data" : {
"Nick" : "Obito",
"xxx" : "xxx"
},
"to" : "token..."
}
-
Messages with both notification and data payload这种消息是在
Notification Message
的基础上加入一些数据,在用户点击通知栏的时候启动对应的activity并传入intent。HTTP POST请求:
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=App Key
{
"notification" : {
"body" : "You have a new message",
"title" : "",
"icon" : "myicon" // Here you can put your app icon name
"click_action": "OPEN_ACTIVITY_1" // should match to your intent filter
},
"data": {
"Nick" : "Obito",
"xxx" : "xxx"
},
"to" : "token..."
}
点击的时候在OPEN_ACTIVITY_1
中拦截
<intent-filter>
<action android:name="OPEN_ACTIVITY_1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
遇到的一些问题
- Firebase API initialization failure.
- Android app not receiving Firebase Notification when app is stopped from multi-task tray
- Push notification works incorrectly when app is on background or not running
- Push not received when app is killed
Firebase控制台测试只能发送Notification,测试的时候把App从最近列表划掉之后能收到,而且是在没翻墙的情况下都能收到。当然当进程被完全杀死就收不到了。data Message则需要通过server api调用,前台后台都能收到透传消息。