android集成fcm消息推送

1.在firebase創建應用,要用到sha-1,如何得到app的sha-1,看上一篇文章。

2.添加依賴

  2.1在app的build中最上面添加

    apply plugin: 'com.google.gms.google-services'

   2.2添加依賴

   //fcm消息通知

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

       implementation 'com.google.firebase:firebase-core:16.0.4'

  2.3在根目錄的build文件中添加 classpath 'com.google.gms:google-services:4.1.0' // google-services plugin

3.創建新文件 MyFirebaseInstanceIDService.kt

 

package com.openfood.rider.ui.service

import android.util.Log
import com.google.firebase.iid.FirebaseInstanceId
import com.google.firebase.iid.FirebaseInstanceIdService

class MyFirebaseInstanceIDService : FirebaseInstanceIdService() {

    /**
     * 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.
     */
    // [START refresh_token]
    override fun onTokenRefresh() {
        // Get updated InstanceID token.
        val refreshedToken = FirebaseInstanceId.getInstance().token
        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)
    }
    // [END refresh_token]

    /**
     *
     * 將token更新到服務器。。。
     *
     * 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 fun sendRegistrationToServer(token: String?) {
        // TODO: Implement this method to send token to your app server.
    }

    companion object {

        private val TAG = "MyFirebaseIIDService"
    }
}

4.創建新文件MyFirebaseMessagingService.kt

package com.openfood.rider.ui.service

import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import com.openfood.rider.utils.LogUtils

class MyFirebaseMessagingService : FirebaseMessagingService() {

    override fun onMessageReceived(p0: RemoteMessage?) {
        LogUtils.e(">>>>>消息來了?")
        super.onMessageReceived(p0)
    }
}

5.在manifest中添加配置

 <!--fcm推送-->
        <service android:name=".ui.service.MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <service android:name=".ui.service.MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>
        <!-- 設置fcm圖標 -->
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/logo" />
        <!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
             notification message -->
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorPrimary" />

6.如果額外增加數據,則在啓動頁getIntent方式獲取數據。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章