gpt4 book ai didi

javascript - 如何将 Google Cloud Messaging 响应获取到 Service worker 的 Chrome 通知事件中

转载 作者:可可西里 更新时间:2023-11-01 11:43:07 25 4
gpt4 key购买 nike

当我使用示例文本进行通知时,正在接收消息并且确实弹出通知。

我已经在 Chrome 上为推送通知设置了一个 Google 帐户,但响应似乎是空的。

服务人员

我在 service worker 上有这个,但它是空的。

self.addEventListener('push', function(event) {
//console.log('Received a push message', event);
console.log(event.data);
});

然后还有另外一件事我用 Fetch 尝试过,使用 localhost url。

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com