简体   繁体   English

Firebase 云消息传递:防止 javascript 客户端中的后台通知

[英]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.

相关问题 Twilio Conversations 使用 Firebase Cloud Messaging 推送通知 - Twilio Conversations push notifications with Firebase Cloud Messaging Firebase云消息传递在客户端的安全性如何? - How secure is firebase cloud messaging at client side? 是否可以使用Firebase Cloud Messaging API列出所有推送通知? - Is it possible to use Firebase Cloud Messaging API to list all push notifications? 如何重新请求在Firebase Cloud Messaging中显示通知的权限? - How to reRequest permission for showing notifications in Firebase Cloud Messaging? 在 Firebase 云消息传递中,React 本机应用程序未通过 TOPIC 接收通知 - React native app not receiving notifications via TOPIC in Firebase cloud messaging 如何使用Firebase云消息传递将推送通知发送到多个设备 - How to send push notifications to multiple devices using Firebase Cloud Messaging JavaScript中的Firebase Cloud Messaging AJAX POST - Firebase Cloud Messaging AJAX POST in JavaScript 无需托管的Firebase云消息传递(Web / JavaScript) - Firebase Cloud Messaging without hosting (Web/JavaScript) Firebase 云消息传递:通知图标背景仅在第二次尝试时有效 - Firebase Cloud Messaging: Notification Icon Background is only working on second attempt 自定义在后台处理程序中运行的Firebase云消息传递的通知 - Customize notification from firebase cloud messaging running in background handler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM