[英]Firebase Cloud Messaging: Prevent background notifications in javascript client
I have a live score display website which is implemented with Google's channel API to push live score updates to the browser.我有一个实时比分显示网站,它是用 Google 的频道 API 实现的,用于将实时比分更新推送到浏览器。 Since Google is shutting down the channel API, I have to move to Firebase Cloud Messaging.由于 Google 正在关闭通道 API,因此我必须转向 Firebase Cloud Messaging。
When I migrated to FCM, I had to add a service worker javascript file (firebase-messaging-sw.js).当我迁移到 FCM 时,我不得不添加一个 service worker javascript 文件 (firebase-messaging-sw.js)。 Whenever a score update is pushed to the browser, if the user is in another browser tab or the user has closed my web page tab, A notification appears to the user.每当向浏览器推送分数更新时,如果用户在另一个浏览器选项卡中或用户已关闭我的网页选项卡,则会向用户显示通知。
I don't need this notification and I want to disable it.我不需要这个通知,我想禁用它。 Also, when user moves to another browser tab, I want to prevent the push message from going into the service worker and route it to my web page, so that when user returns to the tab again, the latest score is updated in the webpage.此外,当用户移动到另一个浏览器选项卡时,我想防止推送消息进入服务工作者并将其路由到我的网页,以便当用户再次返回该选项卡时,网页中的最新分数会更新。
Is there any way to achieve this?有没有办法实现这一目标?
You should pass a parameter (depends what you need to do) in the body of message like this:您应该在消息正文中传递一个参数(取决于您需要做什么),如下所示:
$msg = [
'title' => pushTitle,
'body'=> pushBody,
'icon'=> icon.png,
'image'=> image.png,
'active'=> 1
];
The above is PHP but is also working in the same way-idea in any programming language.以上是 PHP,但在任何编程语言中也以相同的方式工作。 If you use Firebase then you should use cloud functions.如果您使用 Firebase,那么您应该使用云函数。
then in your js file:然后在你的 js 文件中:
messaging.setBackgroundMessageHandler(function(payload) {
console.log('Received background message', payload);
if(payload.data.active == 1){
return;
}
var notificationTitle = payload.data.title;
var notificationOptions = {
body: payload.data.body,
icon: payload.data.icon,
image: payload.data.image
};
return self.registration.showNotification(notificationTitle, notificationOptions);
});
If I had your code(what did you have done until now) I would gave you an exact answer.如果我有你的代码(到目前为止你做了什么),我会给你一个确切的答案。 If you have more questions don't hesitate to ask.如果您有更多问题,请随时提出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.