Android Google FCM云消息集成

一、FCM注意一点:

发送即可收到。

对于 Notification,当app处于前台的是不会接收(app在前台的时候是不会接受推送的,我就被坑了,以为集成有误),至于当app处于后台或者被强杀时依然收到,强杀后能收到。而dataMessage是前后台都能收到。

经测试,美版手机无障碍

二、接入FCM

二.1、前提

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

  • 设备必须是android4.0以上,Google Play Services 必须是 11.2.0以上版本
  • Android SDK Manager 必须有Google Play services SDK
  • Android Studio必须是1.5以上版本

用户手机必须可以连得上Google Play Services 

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

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

创建应用

.
选择添加到安卓应用

.

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

.

二.2.2、添加使用Firebase SDK

添加SDK

项目gradle

应用gradle

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

  • dependencies
  • 在文件的最后添加
    apply plugin: 'com.google.gms.google-services'

配置FireBase SDK

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

当您需要检索当前令牌时,请调用 FirebaseInstanceId.getInstance().getToken()

.
.
消息的接收监听

  • MyFirebaseMessagingService

.

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "FBTEST";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        Log.e("======", "收到推送 From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.e("======", "收到推送 Message data payload: " + remoteMessage.getData());
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.e("======", "收到通知 Message Notification Body: " + remoteMessage.getNotification().getBody());
        }
    }

}

.
token变化的监听

  • MyFirebaseInstanceIDService
    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);
            Log.e("=========", "" + 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.
         * <p>
         * 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.
        }
    
    }
    

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

  • 应用删除实例 ID
  • 应用在新设备上恢复
  • 用户卸载/重新安装应用
  • 用户清除应用数据。

注册service
.清单文件注册service

<service
    android:name=".MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>
<service
    android:name=".MyFirebaseInstanceIDService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>
<meta-data android:name="firebase_messaging_auto_init_enabled"
    android:value="false" />
<meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/ic_launcher_background" />
<meta-data
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/colorAccent" />

配置完成

三、接收推送

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

控制台

.

官方地址:https://firebase.google.com/docs/cloud-messaging/android/client?hl=zh-cn

发布了92 篇原创文章 · 获赞 43 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/Mr___Xu/article/details/83071364