FCM開發(fā)總結(jié)

Android端

先決條件

  • 一臺(tái)運(yùn)行 Android 2.3 (Gingerbread) 或更新版本并運(yùn)行 Google Play 服務(wù) 9.6.1 或更新版本的設(shè)備。

  • SDK Manager需要下載Google play Services SDK

  • Android Studio 1.5 或更高版本

  • Android Studio 項(xiàng)目及其捆綁包名稱。

    2.2 之前的 Android Studio 版本中的 Instant Run 與 Firebase Analytics 不兼容,并且會(huì)阻止其收集某些事件。建議禁用 Instant Run 或升級(jí)到 Android Studio 2.2 +

將Firebase添加至Android項(xiàng)目

  • 首先,要在Firebase console中創(chuàng)建一個(gè)Firebase項(xiàng)目。如果已經(jīng)有一個(gè)與您的移動(dòng)應(yīng)用關(guān)聯(lián)的現(xiàn)有 Google 項(xiàng)目,請(qǐng)點(diǎn)擊 Import Google Project。 否則,請(qǐng)點(diǎn)擊 Create New Project。
  • 點(diǎn)擊 Add Firebase to your Android app 并按設(shè)置步驟進(jìn)行操作。如果在導(dǎo)入現(xiàn)有 Google 項(xiàng)目,這可能是自動(dòng)進(jìn)行的,您只需下載配置文件即可。
  • 按照提示輸入應(yīng)用的包名稱。包名稱很重要,不要填錯(cuò)。
  • 最后,下載google-services.json 文件,然后copy到app目錄下

添加SDK

首先在根級(jí)別的build.gradle文件添加一條規(guī)則。以包含Google服務(wù)插件

   // ...
    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'

然后根據(jù)需求添加Firebase SDK依賴項(xiàng)。上面的com.google.firebase:firebase-core:9.6.1。

上面的依賴firebase如果是從9.6.1升級(jí)到10.0.1,gcm也需要升級(jí)到10.0.1,否則會(huì)出現(xiàn)crash。

Firebase功能庫完整列表:

Gradle 依賴項(xiàng)行 服務(wù)
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,然后根據(jù)提示實(shí)現(xiàn)集成。

設(shè)置FCM客戶端

  • 自定義MyFirebaseMessagingService繼承FirebaseMessagingService,重寫onMessageReceived方法接收通知消息彈通知欄,F(xiàn)CM有兩種消息,data message和 notification message,notification只有在后臺(tái)的時(shí)候才會(huì)走這個(gè)方法,data message不管在后臺(tái)還是前臺(tái)都會(huì)走這個(gè)方法。
  • 自定義MyFirebaseInstanceIDService 集成 FirebaseInstanceIdService用戶token的創(chuàng)建,轉(zhuǎn)換和更新,在onTokenRefresh()方法中獲取token并上傳到服務(wù)器。
  • 獲取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端實(shí)現(xiàn)步驟如下:

  1. 將Firebase添加至服務(wù)器

  2. 實(shí)現(xiàn)連接服務(wù)器協(xié)議

  3. 發(fā)送下游消息(云端至設(shè)備)

FCM有三種消息類型,分別為Notification message,Data Message,Messages with both notification and data payload

  • Notification Message 通知消息,當(dāng)App在前臺(tái)的時(shí)候會(huì)走到我們自定義的MyFirebaseMessagingService 中的 onMessageReceived方法,當(dāng)在后臺(tái)的時(shí)候由系統(tǒng)彈通知欄,當(dāng)app被殺死的時(shí)候,從Firebase后臺(tái)發(fā)送是收不到的。對(duì)應(yīng)的HTTP POST請(qǐng)求:
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 只有當(dāng)App在前臺(tái)或者在后臺(tái)的時(shí)候,才會(huì)走到MyFirebaseMessagingService中的onMessageReceived方法,當(dāng)App被殺死當(dāng)情況下會(huì)不會(huì)走到這里,需要測(cè)試。HTTP POST請(qǐng)求:
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的基礎(chǔ)上加入一些數(shù)據(jù),在用戶點(diǎn)擊通知欄的時(shí)候啟動(dòng)對(duì)應(yīng)的activity并傳入intent。HTTP POST請(qǐng)求:
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..."
}

點(diǎn)擊的時(shí)候在OPEN_ACTIVITY_1中攔截

<intent-filter>
        <action android:name="OPEN_ACTIVITY_1" />
        <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

遇到的一些問題

Firebase控制臺(tái)測(cè)試只能發(fā)送Notification,測(cè)試的時(shí)候把App從最近列表劃掉之后能收到,而且是在沒翻墻的情況下都能收到。當(dāng)然當(dāng)進(jìn)程被完全殺死就收不到了。data Message則需要通過server api調(diào)用,前臺(tái)后臺(tái)都能收到透?jìng)飨ⅰ?/p>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,029評(píng)論 25 709
  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,871評(píng)論 2 45
  • Swift版本點(diǎn)擊這里歡迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh閱讀 26,079評(píng)論 7 249
  • 一天的訓(xùn)練在期盼中終于結(jié)束,動(dòng)作雖然無法清晰的記住,但從未想過一向肢體不協(xié)調(diào)的我也學(xué)起了這種健美操和扇子舞,心...
    叮小咚閱讀 426評(píng)論 0 0
  • 陪他喝酒時(shí),還要負(fù)責(zé)聽他講故事。他喝醉了會(huì)重復(fù)嘮叨一件事,每次講到一半時(shí)他開始哭。一個(gè)三十歲的男人,哭起來竟像個(gè)孩...
    張大怪閱讀 1,011評(píng)論 4 11

友情鏈接更多精彩內(nèi)容