如何使 GCM/FCM 通知类型的消息不可折叠 [英] How to make a GCM / FCM notification-type message non-collapsible

本文介绍了如何使 GCM/FCM 通知类型的消息不可折叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google Cloud Messaging (GCM) 支持两种类型的推送消息:通知"消息和数据"消息.根据文档,通知消息默认是可折叠的,而默认情况下,数据消息是不可折叠的.

为了使数据消息可折叠,您需要指定一个collapseKey.我的问题是:如何使通知消息不可折叠?

注意:该问题也适用于 Firebase 云消息传递 (FCM).

解决方案

消息概念和选项文档 指出:

<块引用>

默认情况下消息不可折叠,通知消息除外,通知消息始终可折叠

但后来在同一页面上,它继续说:

<块引用>

除了通知消息,所有消息都是不可折叠的默认

这有点模棱两可.但是,在有效载荷部分中,它指出:

<块引用>

[通知消息] 可能有可选的数据负载.始终可折叠

因此,似乎不可能使通知消息不可折叠.

我建议这是设计使然,因为在 Android 中创建通知时,它们是 在发布具有相同 ID 的另一个通知时自动替换(类似于折叠消息的工作原理).如果我没记错的话,FCM/GCM 对所有通知消息使用相同的 ID.

可能的解决方案

如果您确实需要不可折叠的通知消息,我建议您发送仅数据有效负载(没有通知或 collapseKey),然后覆盖 onMessageReceived() 来自 FirebaseMessagingService 创建您自己的通知.

Android 快速入门示例:

<块引用>

@Overridepublic void onMessageReceived(RemoteMessage remoteMessage) {//...//检查消息是否包含数据负载.如果 (remoteMessage.getData().size() > 0) {Log.d(TAG, "消息数据负载:" + remoteMessage.getData());//...}//...//此外,如果您打算根据收到的 FCM 生成自己的通知//消息,这里是应该启动的地方.请参阅下面的 sendNotification 方法.}

那里的最后一条评论指向示例 sendNotification() 方法.

对于您的场景,您需要将唯一 ID 传递给 notificationManager.notify() 调用,以便 Android 创建一个新通知并且不替换任何现有通知 - 因此,使消息不可折叠.

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.

解决方案

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.

这篇关于如何使 GCM/FCM 通知类型的消息不可折叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆