gpt4 book ai didi

firebase - 有没有一种方法可以更新 Firebase Cloud Message (FCM) 发送到 Web 应用程序的通知,而不是创建新通知?

转载 作者:行者123 更新时间:2023-12-02 21:45:25 24 4
gpt4 key购买 nike

我正在将 FCM 用于聊天应用程序。每次用户收到消息时,我都会通过 FCM 向他发送通知。如果用户不在前台使用我的 Web 应用程序,消息处理将在默认的 Service Worker 中进行,该 Service Worker 会使用我在请求正文中指定的参数生成通知。问题是,每次发送新的推送消息并且应用程序处于后台时,浏览器(在 Chrome 和 Firefox 上测试)都会创建一条新消息,而不是更新现有消息。我想保留已经可见的通知并仅更新其值,这可能吗?

如果应用程序不在前台,则处理推送的 Service Worker:

importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-messaging.js');

firebase.initializeApp({
'messagingSenderId': '*****'
});

const messaging = firebase.messaging();

发送到 FCM 服务器的请求的 JSON 正文

{
"priority" : "normal",
"collapse_key": "demo",
"notification": {
"title": "Felicius Humble",
"body": "This is a sample message content",
"click_action": "https://****.herokuapp.com",
"icon": "****"
},
"to": "****",
"data": {
"id": 7,
"timestamp": 1493573846692,
"content": "teste",
"type": "MESSAGE",
"direction": "OUTPUT",
"sender": {
"id": 5,
"name": "Felicius Humble",
"email": "****@gmail.com",
"gender": "MALE",
"picture": "****",
"facebookID": "****",
"fcmToken": "****",
"accessToken": "****"
},
"chatID": 3
}
}

最佳答案

好的,我找到了this显示 json 主体可用的所有选项。对于我所需要的,我只需要在 json 正文中的通知 obj 上添加“标签”参数。 如果您想更新通知,请使用相同的标签发送。示例:

{
"notification": {
"title": "",
"body": "",
"click_action": "",
"icon": "",
"tag" : "this must be the same for the notifications you want to update"
},
"to": "****",
"data": {}
}
}

关于firebase - 有没有一种方法可以更新 Firebase Cloud Message (FCM) 发送到 Web 应用程序的通知,而不是创建新通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43709542/

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