Android接入Firebase推送通知

参考:https://firebase.google.com/docs/cloud-messaging/android/client
参考:https://github.com/firebase/quickstart-android/blob/master/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/

Firebase控制台配置

在Firebase控制台创建项目,并配置SHA1,然后下载google-service.json文件并添加到项目的app目录下即可

Gradle添加依赖

implementation 'com.google.firebase:firebase-messaging:17.3.4'

创建FirebaseMessagingService服务并注册

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    /**
     * Called if InstanceID token is updated. This may occur if the security of the previous token had been compromised.
     * Note that this is called when the InstanceID token is initially generated so this is where you would retrieve the token.
     */
    @Override
    public void onNewToken(String token) {
        NLog.i(Constants.TAG, "Refreshed token: " + token);

        // 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(token);
    }

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

        NLog.i(Constants.TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            NLog.i(Constants.TAG, "Message data payload: " + remoteMessage.getData());
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            NLog.i(Constants.TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }
    }
}
<service android:name=".service.MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

获取客户端Token

FirebaseInstanceId.getInstance().getInstanceId()
    .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
        @Override
        public void onComplete(@NonNull Task<InstanceIdResult> task) {
            if (!task.isSuccessful()) {
                NLog.w(TAG, "GetInstanceId failed: ", task.getException());
                return;
            }
            // Get new Instance ID token
            String token = task.getResult().getToken();
            NLog.d(TAG, "Firebase Token: " + token);
        }
    });

注册Topic

FirebaseMessaging.getInstance().subscribeToTopic("activity")
    .addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if (!task.isSuccessful()) {
                NLog.i(TAG, "Subscribe topic failure");
            } else {
                NLog.i(TAG, "Subscribe topic success");
            }
        }
    });

控制台发送消息

在控制台左侧选择Cloud Messaging,然后点击写新消息,即可向指定设备或全部设备发送消息

上一篇 Android接入Google Analytics记录
下一篇 Android接入Firebase推送不执行onMessageReceived方法
目录
文章列表
1 Vue使用axios实现登录验证拦截及页面跳转
Vue使用axios实现登录验证拦截及页面跳转
2
fastjson生成json时Null属性不显示
fastjson生成json时Null属性不显示
3
Spring Cloud使用Nacos作为服务配置中心
Spring Cloud使用Nacos作为服务配置中心
4
Spring Boot开启Druid监控控制台
Spring Boot开启Druid监控控制台
5
golang开启mod后import报红解决
golang开启mod后import报红解决
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。