首页 > 解决方案 > 浏览器通知:未显示

问题描述

我似乎按照 Google Labs 建议的代码创建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-notificationnotificationsprogressive-web-apps

解决方案


这是一个愚蠢的错误。Mac OS X 的通知设置(我认为是默认设置)设置为“请勿打扰”。禁用显示推送通知。想像我这样可能没有意识到这一点的其他人应该知道。


推荐阅读