使用广播频道与Firebase-SW通信,并反应组件不起作用

发布于 2025-02-09 04:24:16 字数 872 浏览 0 评论 0原文

我正在尝试使用Firebase SW中的广播频道在OnbackgroundMessage中发布消息,这是服务工作者中的代码:

messaging.onBackgroundMessage(function (payload) {
 console.log("Received background message ", payload);

 const notificationTitle = payload.notification.title;
 const notificationOptions = {
  body: payload.notification.body,
  icon: "/logo192.png",
 };
 //BROADCAST
 const broadcast = new BroadcastChannel("background-message");
 broadcast.postMessage("broadcast payload");

 return self.registration.showNotification(
 notificationTitle,
 notificationOptions
);

});

在app.js文件中,我设置了Listner,以使用Redux派遣通知的有效载荷:

const broadcast = new BroadcastChannel("background-message");
  broadcast.onmessage = (event) => {
    console.log("broadcast message:", event.data);
    dispatch(getNotifications(event.data));
  };

我尝试推出测试通知,但它不起作用

I'm trying to use broadcast channel inside firebase sw to post messages in the onBackgroundMessage, this is the code inside the service worker:

messaging.onBackgroundMessage(function (payload) {
 console.log("Received background message ", payload);

 const notificationTitle = payload.notification.title;
 const notificationOptions = {
  body: payload.notification.body,
  icon: "/logo192.png",
 };
 //BROADCAST
 const broadcast = new BroadcastChannel("background-message");
 broadcast.postMessage("broadcast payload");

 return self.registration.showNotification(
 notificationTitle,
 notificationOptions
);

});

And in the app.js file I set up the listner to dispatch the payload of the notification using redux:

const broadcast = new BroadcastChannel("background-message");
  broadcast.onmessage = (event) => {
    console.log("broadcast message:", event.data);
    dispatch(getNotifications(event.data));
  };

I tried to push a test notification but its not working

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文