Google 一直很推動Firebase Cloud Messaging (FCM), 想讓開發商的APP從GCM轉移至FCM
不過對我們開著者來說, 可以用就好了嘛~! 真的很懶得做
沒想到google 祭出大招了, 以下是來自google的通知信
沒錯, GCM 會在 2019/4/11停止服務, 相關API與功能也一併會停止, 屆時您的app通知系統就真的沒用了....
以下是來說明, 如何快速地使用FCM
1. 我們先進去 firebase console 並新增專案, 只需輸入project name就好了
2. 進入該專案的dashboard首頁, 並往下捲至 Notification. 並點擊“開始使用”
3.選擇 android/ ios 平台
選完後, 會出現以下畫面
基本上firebase的設定操作先告一段落.
接下來, 我們由Android Studio IDE來做設定
ps: 請先在您的android studio用剛剛在firebase開通的那組帳號做登入.
1. 首先在專案中點擊firebase, 位置在“Tools" -> "Firebase"
2. 點擊後在您的專案中, 右邊會出現assistant 視窗, 請點擊 cloud messaging, 便開始設定fcm
3. 點擊後, 會出現教學視窗, 依序點擊步驟.
4. 連接至Firebase, 有建立新專案 或 選擇現有專案
我們選擇現有專案, 並選擇前面在firebase中新增的專案
connect 成功後會出現提示 如下圖所示
5. 加入FCM 至您的專案中, 基本上點擊後, AS就會幫你把該裝的裝好了, 新增內容如下
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> |
設定接收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了, 如下圖
發送FMC 訊息
最簡單的方式, 到Firebase 發送message
1. 先到firebase console, 並點選 notification 會出現下圖
進入後便會出現設定message的地方
Target
- 針對安裝app的所有人發送訊息,
- 選擇 使用者區隔
- 選擇您的App
- 針對單一裝置
- 選擇單一裝置, 並貼上在log中取得的toke 如下圖:
結果:
相關文章
開啟App時,收不到推播!?
參考文件: