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

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

问题描述

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

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.

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

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?

注意:该问题也适用于Firebase Cloud Messaging(FCM).

Note: The question applies to Firebase Cloud Messaging (FCM) as well.

推荐答案

默认情况下,

消息不可折叠,但通知消息除外,通知消息总是可折叠

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

But then later on the same page, it goes on to say:

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

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

Which is somewhat ambiguous. However, in the payload section, it states:

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

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

Therefore, it doesn't seem possible to make notification messages non-collapsible.

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

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.

如果您确实希望收到不折叠的通知消息,建议您发送纯数据有效载荷(不发送通知或collapseKey),然后覆盖

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.

@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.

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

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天全站免登陆