简体   繁体   English

Firebase 云消息在离开 window 焦点时不显示通知

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

相关问题 React Native & Firebase 云消息:如何避免重复推送通知? - React Native & Firebase Cloud Messaging: How to avoid duplicated push notification? Firebase 云消息:无法在 React-js 中接收通知 - Firebase cloud messaging: Cannot receive notification in React-js firebase 云消息传递给 A bad HTTP 响应代码 (404) 单击“允许”并手动添加服务工作者不起作用 - firebase cloud messaging gives A bad HTTP response code (404) after clicking on 'allow' and adding service worker manually doesn't work 当屏幕处于活动状态时,React Js Firebase Cloud 消息传递未收到通知 - React Js Firebase Cloud messaging not recieving notifications when screen is active 使用Jest测试Firebase云消息传递 - Test firebase cloud messaging with Jest firebase signOut() 方法不退出 - firebase signOut() method doesn't sign out firebase safari 错误消息:此浏览器不支持使用 firebase08ABB354C 所需的 API - firebase safari error Messaging: This browser doesn't support the API's required to use the firebase SDK react-select focus() 更改后不显示 cursor - react-select focus() doesn't show cursor after change 反应 useRef 焦点不在 iPhone 上显示键盘 - react useRef focus doesn't show keyboard on iPhone 当我单击收件箱组件时,它在我的 window 中没有显示任何内容 - When I click on inbox component it doesn't show anything in my window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM