[英]Google Cloud Messaging (GCM) not working with firefox
我的這個Service Worker在Chrome上可以很好地接收通知,但是在Firefox中卻沒有收到。
在firefox中根本不會觸發Push偵聽器(通過調試),
PS:服務工作者已成功注冊,但未收到通知。
我的代碼有什么問題?
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);
})
);
});
根據此文檔 ,如果您正在使用Channel Messaging API與服務工作者通信,請設置一個新的消息通道( MessageChannel.MessageChannel()
),並通過在服務工作者上調用Worker.postMessage()
將port2發送到服務工作者。服務人員,以打開溝通渠道。 您還應該設置一個偵聽器,以響應從服務工作者發回的消息。
確保按照以下步驟正確設置GCM。 您還可以檢查以下相關鏈接: Firefox的GCM等效項
希望這可以幫助!
您將需要在客戶端中共享您的代碼以注冊推送通知。
話雖如此,一旦注冊了推送通知,您將收到帶有端點的訂閱。 在Firefox中,該終結點永遠不會是GCM網址,而是Mozilla提供的推送服務器:
navigator.serviceWorker.ready
.then((reg) => reg.pushManager.subscribe({ userVisibleOnly: true }))
.then((subscription) => {
const endpoint = subscription.endpoint;
// endpoint will have different server values when using this code in Chrome or Firefox.
以下是要考慮的主要注意事項:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.