【发布时间】:2018-08-18 10:49:15
【问题描述】:
我似乎按照 Google 实验室建议的代码创建 serviceWorker 并为 pushing notifications 编写了代码,但它似乎没有出现。似乎来自服务器的推送事件确实到达了浏览器,但是 showNotification 函数被忽略了。
// in sw.js
self.addEventListener('push', function(event) {
console.log('Push Received'); // this shows up
event.waitUntil(self.registration.showNotification('Hello World'));
// tried self.registration.showNotification('Hello World') but still does not show up
});
为了进一步检查showNotification 是否有效,我在main.js 中添加了以下内容。
//in main.js
console.log(Notification.permission) // shows granted
if (Notification.permission == 'granted') {
console.log('will show notification') // this gets logged
navigator.serviceWorker.getRegistration().then(function(reg) {
Console.log('reached here'); // this too gets logged
reg.showNotification('Hello world!');
});
最后我发现了一些人用来测试的这个网站
https://gauntface.github.io/simple-push-demo/
还有
https://web-push-book.gauntface.com/demos/notification-examples/
在允许来自该站点的通知后,我发现使用屏幕或给定的 curl 命令没有显示任何通知。
我在 Chrome 和 Firefox 中都试过了,结果是一样的。我在 Mac OS 10.13.6 上使用 Chrome 68.x 和 Firefox 61.x
我发现我正在使用的网站在 Chrome 设置(高级部分)中启用了通知。即使点击地址栏的secure 部分,也会显示通知已设置为allow。我还缺少其他一些设置吗?谢谢
【问题讨论】:
标签: push-notification notifications progressive-web-apps