我有一个 Service Worker 可以通过 Chrome 很好地接收通知, 但 Firefox 无法接收。
在 Firefox 中根本不会触发 Push 监听器(通过调试),
PS:Service Worker 已成功注册,但未收到通知。
我的代码有什么问题?
self.addEventListener('install', function (event) {
event.waitUntil(self.skipWaiting());
});
self.addEventListener('activate', function (event) {
console.log('Activated', event);
});
self.addEventListener('push', function (event) {
event.waitUntil(
fetch('/path', {
credentials: 'include',
method: 'post',
})
.then(function (response) {
return response.json()
.then(function (data) {
return self.registration.showNotification(data.title, {
body: data.body,
icon: '/images/image.png',
});
});
})
.catch(function (error) {
console.error('wrong', error);
})
);
});
最佳答案
基于此documentation ,如果您使用 Channel Messaging API要与 Service Worker 进行通信,请设置一个新的消息 channel ( MessageChannel.MessageChannel()
) 并通过调用 Worker.postMessage()
将 port2 发送到 Service Worker在 Service Worker 上,以打开通信 channel 。您还应该设置一个监听器来响应从服务工作人员发回的消息。
确保您遵循这些 steps关于如何正确设置 GCM。您还可以查看此相关链接:GCM Equivalent for Firefox
希望这有帮助!
关于javascript - Google 云消息传递 (GCM) 无法与 Firefox 配合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39020868/