Join Joe Marini for an in-depth discussion in this video Types of messages, part of Learning Firebase Cloud Messaging for Android.
- [Instructor] There are essentially two types of messages that you can send with FCM, notification messages and data messages. You can think of notifications as messages that are intended to be seen by the end user of your client app. When your app is in the background, the FCM SDK handles them automatically for you. They are sent to the system tray notification area. When the user taps the notification, your app is launched and any data that was associated with the notification is passed into your app as part of the launch intent.
Notifications contain a predefined set of key value pairs of data and can have an optional data payload associated with them. Notification messages can be sent either programmatically by your app server or by using the Notifications tab within the Firebase console. Data messages are usually intended to be consumed directly by your application. FCM doesn't perform any automatic handling of data messages. Your app must implement a service to receive and process them.
They only contain your custom key value pairs of data and are limited to four K in size. Data messages are sent programmatically by your back-end app server. Notification and data messages have particular formats and key value pairs. For the HTTP JSON format, notification messages typically have a structure that looks something like this. The message is enclosed in a JSON object with a property named message.
Within that object, there's a notification object, along with a destination identifier, in this case a registration token, that identifies a particular client app. The notification property contains properties for the message, including its title, body content, and any other properties. Data messages have a similar format. The main difference is that instead of containing a notification object, they use a data object. This data object encloses your app's custom key value pairs.
FCM messages can also be either collapsible or non-collapsible. A non-collapsible message indicates that each message is important and needs to be delivered to the device. Examples of these might be instant message chats or other critical messages that contain unique content. On Android, there's a limit of 100 messages that can be stored without collapsing. If the limit is reached, all stored messages on the server are discarded. When the device is back online, it receives a special message indicating that the limit was reached and your app can then do a full sync with your server.
A collapsible message is one that might be replaced by a newer message if it hasn't yet been delivered to the device. Usually these are send-to-sync messages, sometimes called a ping, that tells an app to sync data from the server. A good example might be a sports app that gives a user the latest score. In that case, only the most recent message is the one that's relevant. To indicate that a message is collapsible on Android, include the collapse_key parameter in the message payload.
FCM allows a maximum of four different collapse_keys per Android device to be used by the app server at any given time. If you exceed this number, then FCM only keeps four of them and there's no guarantee which ones it keeps. To read more about the full details on the structure and types of FCM messages, refer to this link in the Firebase Cloud messaging documentation. And for the full list of various keys and values that you can include in your FCM message structures, refer to this link in the Firebase docs.