今天要來花兩篇的篇幅來聊聊關於FCM(Firebase Cloud Messgae)的Android實作
FCM是什麼??就是辣個你每天會被通知信煩死的那個功能눈_눈
對,英文就大家有時候會叫他Notification,實際上也就跟今天要描述的東西是同一個
那這個FCM實際上是Google的產品之一,呃~我去查過價錢了,基本上是免費的(笑)
我絕對不會說我這幾天忘了關GCP的VM然後被收了3xxx現在正在凹退款....
而FCM在實作上其實並不算太難,因為很多套件Google都幫你寫好了,甚至其實這方面都有一套完整的教程可以看
不過教程這種東西是這樣,看歸看,但是時常找不到到什麼東西在哪裡才是我的常態(哭)
我有時候也常常不知道為什麼其他開發者都抗得懂我都抗不懂(聳肩)
好啦,那來看一下今天的實作
簡單說一下今天的實作為何
左圖為虛擬手機,由畫面中可以看見手機處於待機狀態
右邊網頁是FCM的測試頁,該網頁可以測試送出FCM通知的狀況
也因此,在右邊點選測試送出後,左方手機就會接到通知,而這部分在實體手機一樣可以運作的,請放心
那接下來這是這次的Github
->https://github.com/thumbb13555/FCM_Example/tree/d398289133650f3d103585c4814a7f00a51b058a
來吧!
1. 在Firebase設置FCM專案
這次的內容其實沒有什麼程式,幾乎都只是FCM網頁的操作居多
那首先,請先來到FireBase的專案選擇頁面(呃...要有Google帳戶歐)
https://console.firebase.google.com/
請按下建立專案後,來到這個頁面
下一步
這部分為求方便,我就不使用分析功能了,到此直接完成建立專案
然後就會來到主畫面,接著請按下Android
再來,假設你已經開啟專案了,請填入PackageName
按下註冊應用程式,接下來他會要求你下載一個json檔案,並放入專案
注意,這個地方如果顯示不出我的樣式,請如下圖操作
接下來他會要你載入SDK,看起來有點複雜,但不用怕我都幫你整理好了~~
請先打開專案中的 build.gradle(Project: FCM_Demo)
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() mavenCentral() } dependencies { classpath "com.android.tools.build:gradle:4.2.2" classpath 'com.google.gms:google-services:4.3.10' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() mavenCentral() jcenter() // Warning: this repository is going to shut down soon } } task clean(type: Delete) { delete rootProject.buildDir }
再來開啟下面的那個build.gradle(Module: FCM_Demo)
plugins { id 'com.android.application' id 'com.google.gms.google-services' } android { compileSdkVersion 30 defaultConfig { applicationId "com.noahliu.fcm_demo" minSdkVersion 23 targetSdkVersion 30 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'com.google.android.material:material:1.4.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.0' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' implementation platform('com.google.firebase:firebase-bom:28.4.0') }
ok,設定的部分就到這邊就完成了,接下來是Android studio編輯器那邊的程式與設定
2. Android Studio中連結FCM
ok,來到編輯器這邊
首先我們要讓編輯器這邊接上FCM系統
請點擊Tools->Firebase
再來在編輯器的右側會跑出這個視窗,請點選Cloud Messaging->Set Up...
OK,接下來他要你一步一步操作....
總之,先讓1跟2變成這樣的狀態吧!按鈕按下去就搞定了XD
再來的東西看不懂也沒關係,照著我的做就好
首先請先為專案新增一個Service
資料夾右鍵->New->Service->Service
OK,進到Service後,請把繼承的Service改成FirebaseMessagingService
然後新增兩個Override,onNewToken以及onMessageReceived
再來,請在下面加個Log,等等來看他都會顯示些什麼
public class FCMService extends FirebaseMessagingService { public static final String TAG = FCMService.class.getSimpleName()+"My"; public FCMService() { } @Override public void onNewToken(@NonNull String s) { super.onNewToken(s); Log.d(TAG, "裝置Token: "+s); } @Override public void onMessageReceived(@NonNull RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); Log.d(TAG, "onMessageReceived: "+remoteMessage.getData()); } }
最後,請進到AndroidManifest.xml新增intent-filter
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.noahliu.fcm_demo"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.FCM_Demo"> <service android:name=".FCMService" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
再來,為了在每次開APP都可以確認到token
我們在MainActivity這邊加入可以確認token的程式碼
如下
public class MainActivity extends AppCompatActivity { public static final String TAG = FCMService.TAG; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FirebaseMessaging.getInstance().subscribeToTopic("news"); FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() { @Override public void onComplete(@NonNull Task<String> task) { if (!task.isSuccessful())return; String token = task.getResult(); Log.d(TAG, "onComplete: "+token); } }); } }
接下來按下執行,就會看見你的token囉!
解釋一下token的部分
token為你在裝好APP後第一次執行時,App會自動產生的一組編號
而這組編號會直到App被刪除為止,一直存在
而這組token很多都會順便拿來當作身份辨識之用,之後也能夠為單一使用者做客製化等等等...反正功能很多XD
接下來回到Firebase網頁
3. 發送第一則消息
接下來就要來嘗試發送一則訊息
請至網站的主頁旁邊的側欄,找到Cloud Messaging
然後點擊主畫面中的Send your message
輸入以下空格,點選傳送測試訊息
在這邊輸入剛才執行完後出現的token碼
點下測試,過一下子就是看到手機接到通知囉!
OK,在上篇的內容,我們完成了一個製作FCM最簡單的流程
那麼在實際上線,當然不可能搞得這麼簡單,對吧!
因此,在下篇文章中,我們要把這篇文章的後半段改為在postman(一個API測試軟體)上操作
並在手機上實作一則完整的通知與點擊等等
那麼,這篇讀完的話,還麻煩請移動到我的FCM下篇哦!
->碼農日常-『Android studio』FCM雲端推播與通知系統(下)-使用Postman發送資料與撰寫通知
老樣子,那文章就到這邊,如果覺得還不錯...