Handle FCM messages on Android

栏目: Android · 发布时间: 6年前

内容简介:In theWhen sending push notifications to your users, notification messages are the preferred method if you want to take advantage of the Firebase Console and let the Android system handle notification posting.When using an FCM notification message, the sy

Source: Handle FCM messages on Android from Firebase

Handle FCM messages on Android
Handle FCM messages on Android

Jingyu Shi

Developer Advocate

In the “Notifying your users with FCM” blog post, we shared guidance on using FCM in modern Android with regard to all the power management features. Following on that, let’s look at the common workflow of FCM messages — notification messages and data messages, and how to handle these messages in your code.

Notification messages

When sending push notifications to your users, notification messages are the preferred method if you want to take advantage of the Firebase Console and let the Android system handle notification posting. Notification messages are high priority by default, and high priority FCM messages will still be delivered to users immediately even when the device is idle.

When using an FCM notification message, the system handles showing the notification on behalf of your app when it’s in the background. When your app is in the foreground, the FCM notification message is delivered to the onMessageReceived() handler and you can handle it by posting a notification if needed or update the app content with the FCM payload data (Max 4KB) or fetch content from app server.

Workflow

An App server sends a notification message (or a notification message with a data payload ) to a user:

Handle FCM messages on Android

  1. App server sends a notification message
    • Notification messages are high priority by default, and collapsible –the following message will replace the current message if it’s not delivered yet.
  2. The FCM message is delivered to the client app
      When your app is in Foreground
      • Developer needs to handle the notification message in the handler.
      • Post a notification or update app content in the FCM callback.
      When your app is in Background
      • Notification delivered to System Tray.
  3. If there’s a notification posted, your user may tap the notification and open your app. You can then use the data in getIntent() to update the app content if needed.

Note: if the user dismisses the notification, data will not be delivered when app opens.

Code sample:

class SimpleFirebaseMessagingService : FirebaseMessagingService() {

   private val TAG = "spFirebaseMsgService"

   override fun onMessageReceived(remoteMessage: RemoteMessage) {

       // when App is in foreground, notification message:
       if (remoteMessage.notification != null) {
           // post notification if needed
           updateContent(remoteMessage.notification)
       }

       // process data payload
   }

   private fun updateContent(notification: RemoteMessage.Notification) {}

}

SimpleFirebaseMessagingService.kt

Data message

You should use data messages if you need to handle the notification in the client app, whether to customize the notification or to decrypt the received payload data.

A data message is normal priority by default, which means it will be batched to the next maintenance window when the device is in Doze , by default.

Handling Messages

When sending data messages, you need to handle the messages in the onMessageReceived() callback in the client app. The suggested approach in the handler is as following:

  1. When you need to notify the user, post a notification first.
  2. Update app content with payload data.
  3. If needed, schedule the work to fetch additional data from app server.

Workflow

An App server sends data messages to notify users with end-to-end encrypted FCM message:

Handle FCM messages on Android

  1. App server sends a data message to FCM server
    • The data message should include all the data needed for a notification in the payload
  2. When the message is received in the FCM app client, you have a short window* to handle the message and post a notification
    1. Handle payload decryption and customized notification in the onMessageReceived() callback, then immediately post a notification to the user
      • Note that FCM does NOT guarantee the order of delivery. Developers should handle it when messages are delivered out of order; when you send non-collapsible messages, you can keep track of the order with a message sequence ID, and hold off notifying the user until all messages are received or you give up waiting in the event the prior messages TTL expired.
    2. Use the payload data to update the client content and store data locally
    3. If you need additional data that exceeds the 4KB FCM payload limit, schedule a job or use WorkManager to fetch the data but don’t block displaying an initial notification on this.
      • WorkManager is recommended when it’s stable
      • Use JobScheduler
    4. If you need to update the notification with data fetched, update the notification if it’s still active
  3. User opens the app
    • If app content is not up-to-date, check if work scheduled in 2.3 is completed, and update your app content accordingly.

Note: Keep in mind that the FCM handler only has approximately 20 seconds once the message is received, as it’s designed to complete short tasks like posting a notification. If you need to process longer than this window, we recommend scheduling a job or use the WorkManager API.

Code sample:

class SimpleFirebaseMessagingService : FirebaseMessagingService() {

   private val TAG = "spFirebaseMsgService"

   override fun onMessageReceived(remoteMessage: RemoteMessage) {

       // Use data payload to create a notification
       if (remoteMessage.data.isNotEmpty()) {

           // step 2.1: decrypt payload
           val notificationMessage = decryptPayload(remoteMessage.data)
           // step 2.1: display notification immediately
           sendNotification(notificationMessage)
           // step 2.2: update app content with payload data
           updateContent(remoteMessage.data)

           // Optional step 2.3: if needed, fetch data from app server
           /* if additional data is needed or payload is bigger than 4KB, App server can send a flag to notify client*/
           if (remoteMessage.data["url"] != null) {
               scheduleJob()
               // use WorkManager when it's stable
           }
       }
       // process notification payload when app in foreground...
   }

   private fun decryptPayload(dataPayload: Map<String, String>): String {
       return "decrypted message"
   }

   private fun sendNotification(notificationMessage: String) {}
   private fun updateContent(dataPayload: Map<String, String>) {}
   private fun scheduleWork() {
       // it's recommended to use WorkManager when it's stable, use JobScheduler
       // on background work complete, update the notification if still active
   }
}

SimpleFirebaseMessagingService.kt

FCM on modern Android

Android has introduced many power improvement features in recent versions, so make sure you review and test your FCM use cases against these features. You can learn more about Android Power features and how it works with FCM from this blog post .

If you are not using FCM yet, it’s time to upgrade. The C2DM library was officially deprecated in 2012 and shut down completely in 2015, making it no longer compatible with modern Android. We also announced in April 2018 that Google Cloud Messaging (GCM) server and client APIs have been deprecated and will be removed as soon as April 11th, 2019. You can find out more about the announcement and migration guides in this blog post .

To take advantage of all the new features and functionality that FCM and Android provide, we recommend using FCM today. To get started, visit the Firebase Cloud Messaging documentation .

除非特别声明,此文章内容采用 知识共享署名 3.0 许可,代码示例采用 Apache 2.0 许可。更多细节请查看我们的 服务条款


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

重新定义管理

重新定义管理

[美]布赖恩·罗伯逊 / 中信出版社 / 2015-10-1 / 45

还没听说过合弄制?你一定会听说的。终于,迎来了一本合弄制创建者的著作,讲解了这一公司经营方式的革命性新系统及其实施方法。 今天的商界,情况瞬息万变。但在绝大多数组织中,最具资格响应变化的人们却几乎都没有权力去做出改变。相反,他们不得不遵守那些由领导们设立的亘古不变的战略,而且这些领导们仍然相信“预测和控制”才是有效管理的关键。 合弄制向你展示了怎样让组织中工作的每一个人都成为一名领导,......一起来看看 《重新定义管理》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

随机密码生成器
随机密码生成器

多种字符组合密码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试