How to make a GCM / FCM notification-type message non-collapsible

眉间皱痕 提交于 2020-02-27 09:05:11

问题


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.


回答1:


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.

Possible solution

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.




回答2:


First off, just going to put the FCM docs reference here:

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.

As per your question -- "How can you make a notification message non-collapsible?" -- have you specifying a different collapse_key for each notification message you send?

A collapsible message works as if each of the message has the same collapse_key. But if you specify a different one for each message, then it won't be able to replace the previous one. This would make a notification message behave like a non-collapsible message.

However, since you are using a collapse_key, it is technically still treated as a collapsible message. Which means it is still a subject to the limit of four collapse keys at a time:

FCM allows a maximum of four different collapse keys per Android device to be used by the app server at any given time. In other words, the FCM server can simultaneously store four different collapsible messages per device, each with a different collapse key. If you exceed this number, FCM only keeps four collapse keys, with no guarantees about which ones are kept.

With all that said, any reason why you're not just using a data message payload? We use it on our app and I find it more flexible than a notification message.



来源:https://stackoverflow.com/questions/50288118/how-to-make-a-gcm-fcm-notification-type-message-non-collapsible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!