[英]Firebase cloud messaging doesn't show notification when out of window focus
Recently started working on FCM and there are two cases, either you're on browser window you'll get an in app notification within the window using an event listener or if you're off the browser window, you'll get a desktop notification.最近开始研究 FCM,有两种情况,要么您使用浏览器 window,您将使用事件侦听器在 window 中收到应用内通知,或者如果您关闭浏览器 Z05B8C74CBD96FBF2DE4C1A352702FBF4,您将收到桌面通知.
In my particular case whenever i'm on window i'm calling an API to fetch data whenever there's a certain message object recieved在我的特殊情况下,每当我在 window 上时,我都会调用 API 以在收到特定消息 object 时获取数据
navigator.serviceWorker.addEventListener("message", ({ data }) => {
dispatch(getNotifications(10));
});
this works fine but when I have an open tab but i'm on another window, FCM doesn't get this message object and no event listener is being invoked, essentially I have to either refresh the page or use window.onfocus to re-call the api when I come back to the tab.这工作正常,但是当我有一个打开的选项卡但我在另一个 window 上时,FCM 没有收到此消息 object 并且没有调用事件侦听器,基本上我必须刷新页面或使用 Z05B8C74CBD96FBF2DE4C1A352702 重新聚焦。当我回到选项卡时,请致电 api。
Anyone with a workaround?有人有解决方法吗?
Firebase will not display the message when you are on another tab, sadly.遗憾的是,当您在另一个选项卡上时,Firebase 不会显示该消息。
But you can use the BroadcastChannel API out of the box.但是您可以使用开箱即用的BroadcastChannel API。 It works like a charm.
它就像一个魅力。 In your service worker you can add this:
在您的服务人员中,您可以添加以下内容:
const broadcast = new BroadcastChannel('background-message');
messaging.onBackgroundMessage(message => {
broadcast.postMessage(message);
})
Then in you app listen to 'background-message':然后在您的应用程序中收听“背景消息”:
const broadcast = new BroadcastChannel('background-message');
broadcast.onmessage = (event) => {
console.log('message from service-worker:', event.data)
};
Update : This doesn't work on safari, but this solution seems to work: https://stackoverflow.com/a/57964685/11827624更新:这在 safari 上不起作用,但这个解决方案似乎有效: https://stackoverflow.com/a/57964685/11827624
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.