FCM 通知和 collapse_key [英] FCM notifications and collapse_key

查看:10
本文介绍了FCM 通知和 collapse_key的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 Firebase 通知控制台向用户的 Android 设备发送通知,我注意到即使我在用户设备离线时发送 10 个不同的通知,一旦用户上线,她/他将收到全部 10 个.

I'm sending notifications to users Android devices though the Firebase Notification console and I notice that even if I send 10 different notifications while the user device is offline, once the user goes online she/he will receive all 10.

但在 Firebase 文档中指出:

However in Firebase documentation it is stated that:

FCM 允许应用服务器在任何给定时间最多使用四个不同的折叠键.换句话说,FCM 连接服务器可以同时为每个设备存储四个不同的可折叠发送同步消息,每个消息具有不同的折叠密钥.如果超过此数量,FCM 仅保留四个折叠键,不保证保留哪些.

FCM allows a maximum of four different collapse keys per device to be used by the app server at any given time. In other words, the FCM connection server can simultaneously store four different collapsible send-to-sync 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.

那么用户不应该只收到 4 个通知吗?我错过了什么吗?(我没有扩展 FirebaseMessagingService,我将通知处理留给 SDK)

So shouldn't the user receive only 4 notifications? Am I missing something? (I am not extending the FirebaseMessagingService, I leave the notification handling to the SDK)

更新:如果您未在 Firebase 通知控制台中指定折叠键,则似乎为通知分配了一个隐式折叠键,即 包名称.一旦我通过点击通知启动应用程序,我已经通过检查 getIntent().getExtras() 键集的所有键/值对来测试这一点.事实上,我得到了一个带有包名值的 collapse_key 键,即使我没有指定一个.

UPDATE: If you don't specify a collapse key in the Firebase notifications console, it seems that an implicit collapse key is assigned to the notification and that is the package name of the app. I've tested that by checking all key/value pairs of the getIntent().getExtras() key set, once I launch the app by tapping on the notification. And indeed, I am getting a collapse_key key with the value of the package name, even if I haven't specified one.

更新 2:我尝试通过扩展 FirebaseMessagingService 来处理通知,以便在应用程序处于前台时接收来自通知控制台的消息.我收到通知消息并手动向用户显示通知.你猜怎么着.折叠键效果很好!即使我使用相同的折叠键发送多个通知,我也会收到一个通知.BUT 这显然只在应用程序处于前台时才会发生,因为 Firebase SDK 在应用程序处于后台时不会调用 onMessageReceived(),而是调用处理通知本身.这是否意味着这是 Firebase SDK 的错误?(因为该问题仅在 SDK 显示通知时发生)

UPDATE 2: I tried to handle the notifications by extending the FirebaseMessagingService, so that I receive the messages from the notifications console, when the app is in the foreground. I receive the notification message and I manually display a notification to the user. And guess what. Collapse keys work great! I receive a single notification even if I send multiple notifications with the same collapse key. BUT this happens obviously only when the app is in the foreground, because the Firebase SDK doesn't call the onMessageReceived() when the app is in the background, but instead it handles the notification itself. Does that mean that this is a bug of the Firebase SDK? (since the issue happens only when the notification is shown by the SDK)

所以问题仍然存在,为什么我会收到所有 10 个通知,因为每个通知都有相同的折叠键?可能是 FCM 错误?

So question remains, why do I receive all 10 notifications since each notification has the same collapse key? Maybe an FCM bug?

推荐答案

看完帖子和评论,我对所有尝试过的东西,哪些努力成功,哪些失败,我并不完全清楚.如果有项目,我会介绍一些数字,希望对您有所帮助.

After reading the post and comments, I'm not completely clear on everything that has been tried, which efforts were successful and which failed. I'll cover a number if items and hope something is helpful.

您的帖子表明,对于某些测试,您在 Firebase 控制台中编写消息时指定了折叠键.这是不可能的.如果您打开 高级选项 并在 自定义数据 下输入键/值对,这将不起作用.这些值存储在 data 键下的消息中,而不是 collapse_key 必须出现的消息的顶层.此外,文档中的表 1包括警告 data 键不应是表中的任何保留字,特别引用 collapse_key:

Your post indicates that for some tests, you specified a collapse key when you composed the message in Firebase console. That is not possible. If you opened Advanced options and entered a key/value pair under Custom data, that will not work. Those values are stored in the message under the data key, not at the top level of the message where collapse_key must appear. Also, Table 1 in the documentation includes a warning that data keys should not be any of the reserved words in the table, specifically citing collapse_key:

键不应是保留字(from"或任何开头的字)与谷歌"或gcm").不要使用本文中定义的任何词语表(例如 collapse_key).

The key should not be a reserved word ("from" or any word starting with "google" or "gcm"). Do not use any of the words defined in this table (such as collapse_key).

如您帖子的评论中所述,控制台会自动分配一个作为包名称的折叠键,因此不需要用户输入折叠键.

As noted in the comments to your post, the console automatically assigns a collapse key that is the package name, so user entry of a collapse key is not needed.

也就是说,我对控制台的体验与您的一致.我通过仅输入 消息文本 和设备令牌来创建消息.我看不到崩溃处理;设备接收每条消息.根据我对下面描述的测试的经验,这似乎是控制台的问题,而不是一般的折叠处理问题.这很奇怪,因为如果我在应用程序处于前台并调用 onMessageReceived() 时发送消息,我有调试日志记录,使用 getCollapseKey().该输出确认密钥存在并且是我的应用程序包名称.

That said, my experiences with the console match yours. I create messages by entering only Message text and the device token. I see no collapse processing; each message is received by the device. Based on my experience with the tests described below, this seems to be a problem with the console and not with collapse processing in general. This is odd, because if I send the messages when the app is in the foreground, and onMessageReceived() is invoked, I have debug logging that outputs the collapse key in the message using getCollapseKey(). That output confirms that the key is present and is my app package name.

您表示您进行了一些从云功能发送通知的测试.我使用此云功能进行了自己的测试,并观察到预期的消息崩溃:

You indicate that you did some tests sending notifications from a cloud function. I did my own testing with this cloud function and observed the expected message collapse:

exports.test = functions.database.ref('/test').onWrite(event => {
  const token = 'dK1FjGbNr6k:APA91bH7Vz3x...icGO56sJ7rAqOXRI';

  console.log('Sending notification...');

  const payload = {
    notification: {
      title: 'Message',
      body: 'Just one please!'
    }
  };

  const options = {
    collapseKey: 'green'
  };

  return admin.messaging().sendToDevice(token, payload, options).then(response => {
    console.log('Done');
  });
});

我还使用浏览器应用 Advanced Rest Client 发送了此消息,并且还看到正确的消息折叠:

I also sent this message using browser app Advanced Rest Client, and also saw correct message collapsing:

{
  "to": "dK1FjGbNr6k:APA91bH7Vz3x...O56sJ7rAqOXRI",
  "collapse_key": "green",
  "notification": {
    "title": "Message",
    "body": "Just one please!"
  }
}

我还将分享 Firebase 在收到通知消息时会发出分析日志消息.当您想要获取收到的消息计数时,这对于测试很有用:

I'll also share that Firebase emits an analytics log message when a notification message is received. This is useful for testing, when you want to get a count of messages received:

D/FA: Logging event (FE): notification_receive(_nr), Bundle[{firebase_event_origin(_o)=fcm, message_device_time(_ndt)=0, message_time(_nmt)=1498227476, message_id(_nmid)=6447126672094369335}]

这篇关于FCM 通知和 collapse_key的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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