Google Cloud Messaging (GCM) support two types of push messages: "notification" messages and "data" messages. According to the documentation, notification messages are collapsible by default, while data messages are non-collapsible by default.
In order to make a data message collapsible, you need to specifiy a collapseKey
. My question is: How can you make a notification message non-collapsible?
Note: The question applies to Firebase Cloud Messaging (FCM) as well.
Firebase Cloud Messaging (FCM), formerly known as Google Cloud Messaging (GCM), is a cross-platform cloud solution for messages and notifications for Android, iOS, and web applications, which as of June 2022 can be used at no cost.
The first step in GCM is that a third-party server (such as an email server) sends a request to Google's GCM server. This server then sends the message to your device, through that open connection. The Android system looks at the message to determine which app it's for, and starts that app.
Data Payload: Data messages have to be handled by the android app. You can add this kind of messages if you want to send some only data along with the notification. It contains custom key value pairs. {
The collapse key collapses only the notifications bearing the same collapse key. If a user sends 10 notifications with the same collapse key (for example, "SCORE") and 2 notifications without any collapse key and they are not received by the device as the device was offline.
FCM automatically displays the message to end-user devices on behalf of the client app. Notification messages have a predefined set of user-visible keys and an optional data payload of custom key-value pairs.
Implement a GCM listener service that receives remote messages from the app server through GCM. This app will use a new GCM feature known as topic messaging. In topic messaging, the app server sends a message to a topic, rather than to a list of individual devices.
A non-collapsible message denotes that each individual message is delivered to the device. A non-collapsible message delivers some useful content, as opposed to a collapsible message like a content-free "ping" to the mobile app to contact the server to fetch data. FCM does not guarantee the order of delivery.
Using FCM for Push Notifications Firebase Cloud Messaging is required for all managed and bare workflow Android apps made with Expo, unless you're still running your app in the Expo Go app. To set up your Expo Android app to get push notifications using your own FCM credentials, follow this guide closely.
The message concepts and options documentation states:
messages are non-collapsible by default except for notification messages, which are always collapsible
But then later on the same page, it goes on to say:
except for notification messages, all messages are non-collapsible by default
Which is somewhat ambiguous. However, in the payload section, it states:
[notification messages] may have optional data payload. Always collapsible
Therefore, it doesn't seem possible to make notification messages non-collapsible.
I'd suggest that this is by design, because when creating notifications in Android, they are automatically replaced when another notification with the same ID is posted (similarly to how collapsing messages works). If I remember correctly, FCM/GCM uses the same ID for all notification messages.
If you do want a non-collapsible notification message, I'd suggest sending a data-only payload (with no notification or collapseKey
), and then overriding the onMessageReceived()
from the FirebaseMessagingService
to create your own notification.
There is an example of this available on the Android quickstart sample:
@Override public void onMessageReceived(RemoteMessage remoteMessage) { // ... // Check if message contains a data payload. if (remoteMessage.getData().size() > 0) { Log.d(TAG, "Message data payload: " + remoteMessage.getData()); // ... } // ... // 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. }
The last comment there points you to the example sendNotification()
method.
For your scenario, you'll need to pass a unique ID to the notificationManager.notify()
call so that Android creates a new notification and does not replace any existing notifications - therefore, making the message non-collapsible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With