push-notification - 浏览器通知:未显示
问题描述
我似乎按照 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
。我还缺少其他一些设置吗?谢谢
解决方案
这是一个愚蠢的错误。Mac OS X 的通知设置(我认为是默认设置)设置为“请勿打扰”。禁用显示推送通知。想像我这样可能没有意识到这一点的其他人应该知道。
推荐阅读
- c - C中结构内的结构
- gremlin - 使用类型间接连接顶点
- angular - Angular:具有管理后端的应用程序的正确项目结构
- sprite-kit - Spritekit 物理销毁对象动画
- apache-spark - Azure HDInsight 的 SparkRunner 上的 Apache Beam 管道
- javascript - 为什么这个最低公分母函数对于更高的数值输入返回未定义
- perl - 在 FASTQ 中合并具有不同标题顺序的 Fasta 和 Qual 文件
- elm - 如何使用 elm-bootstrap 4.1.0 制作条纹表?
- javascript - CKEDITOR - 插入 html 文本或 img 单击 CKEDITOR 外部的某些 DIV
- javascript - Firebase With Web:在参考中使用用户 ID 会导致错误:未捕获的 TypeError:无法读取未定义的属性“on”