当我使用示例文本进行通知时,正在接收消息并且确实会弹出通知。
我已在 Chrome 上设置了一个用于推送通知的 Google 帐户,但响应似乎为空。
服务人员
我在 Service Worker 上有这个,但它是空的。
self.addEventListener('push', function(event) {
//console.log('Received a push message', event);
console.log(event.data);
});
然后我还尝试使用 localhost url 进行 Fetch 的另一件事。
var url = 'http://localhost/notification/index.php?type=fg';
self.addEventListener('push', function(event) {
event.waitUntil(fetch(url).then(function(response) {
console.log(response);
return response.json();
}).then(function(data) {
console.log(data);
//'data' does't have the json from the url
})
)
});
最佳答案
Chrome 尚不支持推送有效负载,因此第一个代码段仅适用于 Firefox ( https://serviceworke.rs/push-payload.html )。
您的第二种方法应该适用于任何浏览器 ( https://serviceworke.rs/push-get-payload.html )。
关于javascript - 如何将 Google Cloud Messaging 响应放入 Service Worker 的事件中以获取 Chrome 中的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35376981/