close

Google 一直很推動Firebase Cloud Messaging (FCM), 想讓開發商的APP從GCM轉移至FCM

不過對我們開著者來說, 可以用就好了嘛~! 真的很懶得做

沒想到google 祭出大招了, 以下是來自google的通知信

螢幕快照 2018-07-02 上午7.09.17.png

 

沒錯, GCM 會在 2019/4/11停止服務, 相關API與功能也一併會停止, 屆時您的app通知系統就真的沒用了....

以下是來說明, 如何快速地使用FCM

1. 我們先進去 firebase console 並新增專案, 只需輸入project name就好了

螢幕快照 2018-07-02 上午7.18.48.png

 

2. 進入該專案的dashboard首頁, 並往下捲至 Notification. 並點擊“開始使用”

螢幕快照 2018-06-12 上午12.00.12.png

3.選擇 android/ ios 平台

 螢幕快照 2018-06-12 上午12.00.24.png

選完後, 會出現以下畫面

螢幕快照 2018-06-12 上午12.00.34.png

基本上firebase的設定操作先告一段落.

 

接下來, 我們由Android Studio IDE來做設定

ps: 請先在您的android studio用剛剛在firebase開通的那組帳號做登入.

1. 首先在專案中點擊firebase, 位置在“Tools" -> "Firebase"

螢幕快照 2018-06-11 下午11.56.32.png

2. 點擊後在您的專案中, 右邊會出現assistant 視窗, 請點擊 cloud messaging, 便開始設定fcm

 螢幕快照 2018-06-11 下午11.56.45.png

3. 點擊後, 會出現教學視窗, 依序點擊步驟.

 螢幕快照 2018-06-11 下午11.57.05.png

4. 連接至Firebase, 有建立新專案 或 選擇現有專案

螢幕快照 2018-07-02 上午7.41.28.png

我們選擇現有專案, 並選擇前面在firebase中新增的專案

connect 成功後會出現提示 如下圖所示

螢幕快照 2018-06-12 上午12.04.00.png

5. 加入FCM 至您的專案中, 基本上點擊後, AS就會幫你把該裝的裝好了, 新增內容如下

螢幕快照 2018-06-12 上午12.04.06.png

6. 設定service, 設定完FCM library 後就該設定 service 部分, for 產生token與接收FCM訊息

取得Token 

  • 建立一個service 他必需繼承FirebaseInstanceIdService
  • 並在 manifest 中, 設定service
<service
    android:name=".MyFirebaseInstanceIDService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>

 螢幕快照 2018-06-12 上午12.08.05.png

設定接收FCM訊息

  • 建立一個service 並繼承FirebaseMessagingService 
  • 並在manifest中, 設定service
    <service
        android:name=".MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>

    基本上, 按照上面所說就差不多了

附上我的grable

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation ('com.google.firebase:firebase-messaging:11.0.4'){
        exclude group: 'com.android.support', module: 'appcompat-v7'
    }

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

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

安裝開啟APP,  便會取得token了, 如下圖

螢幕快照 2018-06-12 上午12.19.45.png

 

發送FMC 訊息

最簡單的方式, 到Firebase 發送message

1. 先到firebase console, 並點選 notification 會出現下圖

螢幕快照 2018-06-12 上午12.12.06.png

進入後便會出現設定message的地方

螢幕快照 2018-07-04 上午12.55.01.png

Target 

  • 針對安裝app的所有人發送訊息, 
    • 選擇 使用者區隔
    • 選擇您的App
       
  • 針對單一裝置
  • 選擇單一裝置, 並貼上在log中取得的toke 如下圖:

螢幕快照 2018-07-04 上午12.59.58.png

 

結果:

Screenshot_20180612-001721_Samsung Experience Home.jpg

 

 

相關文章

開啟App時,收不到推播!?

 

參考文件:

Set Up a Firebase Cloud Messaging Client App on Android

arrow
arrow

    Owen Chen 發表在 痞客邦 留言(2) 人氣()