使用广播频道与Firebase-SW通信,并反应组件不起作用
我正在尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论