在Javascript中使用Firebase时,如何将数据从setBackgroundMessageHandler()传递到angular应用程序?
我在angular应用程序中使用firebase在web中发送推送通知。我实现了在Javascript中使用Firebase时,如何将数据从setBackgroundMessageHandler()传递到angular应用程序?,javascript,angularjs,firebase,firebase-cloud-messaging,Javascript,Angularjs,Firebase,Firebase Cloud Messaging,我在angular应用程序中使用firebase在web中发送推送通知。我实现了消息传递.onMessage()用于在应用程序位于前台时接收通知和消息传递.setBackgroundMessageHandler()当应用程序位于后台时。我收到通知时没有任何问题,但我需要根据收到的通知更新值。我可以在使用消息时进行更新。onMessage()但在使用消息时如何进行更新。setBackgroundMessageHandler()。 即使我可以在本地存储接收到的值并在我的应用程序中获取,也没问题 我在
消息传递.onMessage()
用于在应用程序位于前台时接收通知和消息传递.setBackgroundMessageHandler()
当应用程序位于后台时。我收到通知时没有任何问题,但我需要根据收到的通知更新值。我可以在使用消息时进行更新。onMessage()
但在使用消息时如何进行更新。setBackgroundMessageHandler()
。
即使我可以在本地存储接收到的值并在我的应用程序中获取,也没问题
我在很多地方做过研究,但我找不到任何解决方案。
有人能帮我吗?
谢谢
在搜索了几天之后,找到了一个包含解决方案的Github页面 您可以做的是获取一个窗口客户端列表,该列表将返回您的源站点的选项卡列表,然后向每个窗口客户端发布消息。(此代码将位于
setBackgroundMessageHandler()
中)
要获取页面列表,请执行以下操作:
const promiseChain = clients.matchAll({
type: 'window',
includeUncontrolled: true
})
.then((windowClients) => {
for (let i = 0; i < windowClients.length; i++) {
const windowClient = windowClients[i];
windowClient.postMessage(data);
}
})
.then(() => {
return registration.showNotification('my notification title');
});
return promiseChain;
感谢很多在Github做出贡献的人。
有关更多参考,请访问
希望这对你们有帮助。如果在实施过程中遇到任何疑问,请Ping或评论我。您不知道这为我节省了多少时间,非常感谢。
const promiseChain = clients.matchAll({
type: 'window',
includeUncontrolled: true
})
.then((windowClients) => {
for (let i = 0; i < windowClients.length; i++) {
const windowClient = windowClients[i];
windowClient.postMessage(data);
}
})
.then(() => {
return registration.showNotification('my notification title');
});
return promiseChain;
navigator.serviceWorker.addEventListener('message', function(event) {
console.log('Received a message from service worker: ', event.data);
});