Android开发程序员Android开发学习

Google FCM 离线推送简明教程

2017-08-25  本文已影响6034人  阿敏其人

FCM,即Firebase Cloud Messaging


Firebase,Firebase是一家实时后端数据库创业公司,它能帮助开发者很快的写出Web端和移动端的应用。自2014年10月Google收购Firebase以来,用户可以在更方便地使用Firebase的同时,结合Google的云服务。

Google在今天2016的I/O大会上发表新版Firebase,Firebase是专为行动应用开发者所提供的后端服务平台(Backend as a Services,BaaS)。新版Firebase增加了免费的数据分析工具、云端讯息推播、通知系统、当机报告、远端配置及动态连结等功能,是继2014年10月Google收购Firebase后,18个月以来的一次大改版。

Android Studio2.2中开始支持Firebase服务,如AdMob、分析、认证和通知等。

一、FCM初认识

我们知道,当我们应用被强杀之后,基本上就是收不到推送消息啦,或者说叫离线推送。不管是极光,腾讯信鸽还是阿里推送等,都是基本上对强杀无解,虽然第三方推送有各种奇技淫巧,但是只是杯水车薪。

而iOS在离线消息十分靠谱,那是因为每一个苹果可以通过自家服务器维持一个长链接,每一个iOS的推送都必须和苹果打交道,所以这个过程控制得很好。

Google的自己也是维护一套推送系统,境外开发者基本推送走的是Google的服务,但是由于哈哈哈哈的关系,在国内我们无法使用Google的服务,收不到Google的推送,也因此特殊环境下第三方推送群起,但是强杀之后收到离线消息依然弱爆,除了Google。

Google以前的推送叫GCM,后来收购FireBase退出的FCM是GCM的升级版,本质用的还是GCM.

二、接入FCM

二.1、前提

想要使用FCM,需要满足如下几个条件

其中的难处,就是用户手机必须可以连得上Google Play Services ,这个问题嘛,交给历史,如果你的app是面向境外的,那么一切是如此地丝滑。

二.2、 接入

什么都别说啦,赶紧看一下Firebase FCM接入文档

本文是按照文档接入测试的,你可以自己去看啊,没错啊,到这里你就可以选择关闭页面,如果你愿意看,那么,continue。

二.2.1、在Firebase控制台添加自己的应用

首先你得有一个Google账号嘛,然后登录Firebase的控制台

创建应用

image.png

.
选择添加到安卓应用


image.png

.

下载生成的文件名为 “google-services.json”的文件,放在应用级别的文件目录下

image.png

.

image.png

期间有个添加包名和sha-1值的过程,此时添加的是debug的sha-1,最好在添加完成之后进入设置把release的sha-1值也添加进去。然后重新下载更新的“google-services.json”,重新替换到项目里面。

.

二.2.2、添加使用Firebase SDK

以当前工程没使用过Google Services为例子
需要在工程级gradle文件和应用级的gradle文件上操作。

添加SDK

项目gradle

这点官网无涉及,但是没加可能会报异常
Failed to resolve: com.google.firebase:firebase-messaging:11.2.0

image.png

.
.

应用gradle

    compile 'com.google.firebase:firebase-messaging:11.2.0'
    compile 'com.google.gms:google-services:3.1.0'

.

这样代码不要有洁癖放在顶部,想着所有的apply plugin都放一起,不然很大可能报大概如下错误

Please fix the version conflict either by updating the version of the google-services plugin (information about the latest version is available at https://bintray.com/android/android-tools/com.google.gms.google-services/) or updating the version of com.google.android.gms to 9.0.0. 

.

image.png

题外话
如果同时也引入google map,firebase的版本要一致,比如如下

    // 下方的firebase-messaging 和 play-services-maps 和 play-services版本号必须一致
    compile 'com.google.firebase:firebase-messaging:11.2.0'
    compile 'com.google.android.gms:play-services-maps:11.2.0'
    compile 'com.google.android.gms:play-services:11.2.0'// 添加之
    compile 'com.google.gms:google-services:3.1.0'

配置FireBase SDK

唯一Token的获取和上传
最初启动您的应用时,FCM SDK 会为客户端应用实例生成一个注册令牌。如果您希望定位单台设备或创建设备组,则需要通过继承 FirebaseInstanceIdService
来访问此令牌。

当您需要检索当前令牌时,请调用 FirebaseInstanceId.getInstance().getToken()
如果令牌尚未生成,此方法将返回 null。

.
.
消息的接收监听

/**
 * User: LJM
 * Date&Time: 2017-08-25 & 10:50
 * Describe: 消息接收服务
 * 推送分为 dataMessage和notification 两种
 */
public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "FBTEST";

    @Override
    public 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());
        }
    }

}

.
.
token变化的监听

/**
 * User: LJM
 * Date&Time: 2017-08-25 & 10:48
 * Describe: 实例化令牌服务
 * 处理创建,轮询更新Token,当我们推送到执行设备或者设备组的时候就需要用到这个
 */
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "FBTEST";

    @Override
    public void onTokenRefresh() {
        super.onTokenRefresh();
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "刷新Token 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);
    }

    /**
     * Persist token to third-party servers.
     *
     * Modify this method to associate the user's FCM InstanceID token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        // TODO: Implement this method to send token to your app server.
    }

}

注册令牌可能会在发生下列情况时更改:

所以Firebase强烈建议我们复写onTokenRefresh方法并且及时上传更新的Token通知自家服务器

注册service
.清单文件注册service


        <service android:name=".service.MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>

        <service
            android:name=".service.MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>

        <!--指定图标 可选-->
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/shownf" />

        <!--指定颜色 可选-->
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorPrimary" />

.
.
至此,配置完成,就等着接消息啦。

三、接收推送

控制台发消息,只可以发Notification,但是也足够我们测试啦
(dataMessage必须服务器代码发)

控制台

.

image.png

发送即可收到。

对于 Notification,当app处于前台的是不会接收,至于当app处于后台或者被强杀时收到,嗯,强杀后能收到。而dataMessage是前后台都能收到。

经测试,Pixel 运行Oreo 完美接收,但是如果是其他厂商的机型,想要强杀后继续收到离线消息,一般需要在设置一下


设置方式引自极光推送文档,侵删


对应设置后,华为荣耀7,P9,�三星等均能接收离线消息。

四、服务器

参见文档

end

上一篇 下一篇

猜你喜欢

热点阅读