gpt4 book ai didi

firebase - FCM 通知和 collapse_key

转载 作者:行者123 更新时间:2023-12-04 04:20:45 30 4
gpt4 key购买 nike

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

但是在 Firebase 文档中指出:

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)

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

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

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

最佳答案

阅读帖子和评论后,我并不完全清楚尝试过的一切,哪些努力是成功的,哪些是失败的。如果项目,我将涵盖一个数字,并希望有所帮助。

您的帖子表明,对于某些测试,您在 Firebase 控制台中编写消息时指定了折叠键。这是不可能的。如果您打开了高级选项并在自定义数据下输入了一个键/值对,那将不起作用。这些值存储在 data 下的消息中。键,不在消息的顶层collapse_key必须出现。另外,Table 1 in the documentation包括警告 data键不应是表中的任何保留字,特别是引用 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).



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

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

您表示您做了一些测试,从云功能发送通知。我对这个云函数进行了自己的测试,并观察到预期的消息崩溃:
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 发送了此消息,并且还看到了正确的消息折叠:
{
"to": "dK1FjGbNr6k:APA91bH7Vz3x...O56sJ7rAqOXRI",
"collapse_key": "green",
"notification": {
"title": "Message",
"body": "Just one please!"
}
}

我还将分享 Firebase 在收到通知消息时发出分析日志消息。这对于测试很有用,当您想要获取收到的消息计数时:
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}]

关于firebase - FCM 通知和 collapse_key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44552291/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com