| 選取平台: | iOS+ Android 網頁 Flutter Unity C++ |
如要接收訊息,可以使用擴充
FirebaseMessagingService的服務。
您的服務應覆寫 onMessageReceived 和 onDeletedMessages 回呼。如需完整範例,請參閱 Firebase 雲端通訊快速入門範例。
大多數訊息類型都會提供 onMessageReceived,但有以下例外狀況:
應用程式在背景執行時傳送的通知訊息。在這種情況下,通知會傳送到裝置的系統匣。根據預設,使用者輕觸通知後,系統會開啟應用程式啟動器。
在背景收到同時包含通知和資料酬載的訊息。在這種情況下,通知會傳送至裝置的系統匣,資料酬載則會傳送至啟動器 Activity 的意圖額外項目。
簡單來說:
| 應用程式狀態 | 通知 | 資料 | 兩者並用 |
|---|---|---|---|
| 前景 | onMessageReceived |
onMessageReceived |
onMessageReceived |
| 背景 | 系統匣 | onMessageReceived |
通知:系統匣資料:意圖的額外資訊。 |
如要進一步瞭解訊息類型,請參閱「通知和資料訊息」。
onMessageReceived 回呼的執行時間範圍很短,許多因素都會影響這個時間範圍的長度,包括 OS 延遲、應用程式啟動時間、主執行緒遭到其他作業阻斷,或先前的 onMessageReceived 呼叫耗時過長。
因此,您應避免在 onMessageReceived 中執行長時間執行的工作 (例如從伺服器擷取圖片以顯示在通知中),而應使用 WorkManager 排定工作,處理任何可能需要幾秒鐘才能完成的工作。如要進一步瞭解訊息優先順序及其對處理程序的影響,請參閱「處理高優先順序和一般優先順序訊息」。
編輯應用程式資訊清單
如要使用 FirebaseMessagingService,請在應用程式資訊清單中加入下列項目:
<service
android:name=".java.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
建議您設定預設值,自訂通知的外觀。您可以指定自訂預設圖示和自訂預設顏色,當通知酬載中未設定對等值時,系統就會套用這些預設值。
在 application 標記中加入以下幾行,設定自訂預設圖示和自訂顏色:
<!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
See README(https://goo.gl/l4GJaQ) for more. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_ic_notification" />
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
notification message. See README(https://goo.gl/6BKBk7) for more. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
Android 會顯示並使用下列項目的自訂預設圖示:
從 Firebase 控制台的通知編輯器傳送的所有通知訊息。
未在通知酬載中明確設定圖示的任何通知訊息。
如果未設定自訂預設圖示,也未在通知酬載中設定圖示,Android 會顯示白色應用程式圖示。
覆寫 onMessageReceived
覆寫 FirebaseMessagingService.onMessageReceived 方法後,您就能根據收到的 RemoteMessage 物件執行動作,並取得訊息資料:
Kotlin
override fun onMessageReceived(remoteMessage: RemoteMessage) { // TODO(developer): Handle FCM messages here. // Not getting messages here? See why this may be: https://goo.gl/39bRNJ Log.d(TAG, "From: ${remoteMessage.from}") // Check if message contains a data payload. if (remoteMessage.data.isNotEmpty()) { Log.d(TAG, "Message data payload: ${remoteMessage.data}") // Check if data needs to be processed by long running job if (needsToBeScheduled()) { // For long-running tasks (10 seconds or more) use WorkManager. scheduleJob() } else { // Handle message within 10 seconds handleNow() } } // Check if message contains a notification payload. remoteMessage.notification?.let { Log.d(TAG, "Message Notification Body: ${it.body}") } // Also if you intend on generating your own notifications as a result of a received FCM // message, here is where that should be initiated. See sendNotification method below. }
Java
@Override public void onMessageReceived(RemoteMessage remoteMessage) { // TODO(developer): Handle FCM messages here. // Not getting messages here? See why this may be: https://goo.gl/39bRNJ Log.d(TAG, "From: " + remoteMessage.getFrom()); // Check if message contains a data payload. if (remoteMessage.getData().size() > 0) { Log.d(TAG, "Message data payload: " + remoteMessage.getData()); if (/* Check if data needs to be processed by long running job */ true) { // For long-running tasks (10 seconds or more) use WorkManager. scheduleJob(); } else { // Handle message within 10 seconds handleNow(); } } // Check if message contains a notification payload. if (remoteMessage.getNotification() != null) { Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); } // Also if you intend on generating your own notifications as a result of a received FCM // message, here is where that should be initiated. See sendNotification method below. }
覆寫 onDeletedMessages
在某些情況下,FCM 可能不會傳送訊息。如果裝置連線時,應用程式在特定裝置上待處理的訊息過多 (超過 100 則),或是裝置超過一個月未連線至 FCM,就會發生這種情況。在這種情況下,你可能會收到回撥電話,號碼為 FirebaseMessagingService.onDeletedMessages()。應用程式執行個體收到這個回呼時,應與應用程式伺服器執行完整同步。如果您在過去 4 週內未向該裝置上的應用程式傳送訊息,FCM 就不會呼叫 onDeletedMessages()。
在背景執行的應用程式中處理通知訊息
應用程式在背景執行時,Android 會將通知訊息導向系統匣。使用者輕觸通知後,預設會開啟應用程式啟動器。
這包括同時包含通知和資料酬載的訊息 (以及從 Firebase 控制台的通知編寫器傳送的所有訊息)。在這種情況下,通知會傳送至裝置的系統匣,資料酬載則會傳送至啟動器 Activity 意圖的額外資料。
如要深入瞭解訊息傳送至應用程式的情形,請前往 Firebase 控制台的「DevOps 和參與度」>「訊息」>「報告」資訊主頁。這個資訊主頁會記錄在 Apple 和 Android 裝置上傳送及開啟的訊息數量,以及 Android 應用程式的「曝光」資料 (使用者看到的通知)。