[英]service worker show notification not working
navigator.serviceWorker.register('/service-worker.js').then((reg) => {
})
Notification.requestPermission(function(result) {
console.log('User Choice', result);
if (result !== 'granted') {
console.log('No notification permission granted!');
} else {
navigator.serviceWorker.ready
.then(function(swreg) {
console.log("blaxblux");
swreg.showNotification('Successfully subscribed!', {body:'TEST'});
});
}
});
这是我在main.js
代码
它涉及到console.log('blaxblux')
,但根本不显示通知。 请求权限在浏览器显示带有允许按钮的弹出窗口时起作用。
什么事情可能是问题?
(我使用的是最新版本的 chrome)
我遇到了类似的问题,但在我的 Windows 机器上为 Google Chrome 关闭了通知。 我继续启用通知,他们神奇地开始工作。
Settings -> System -> Notifications & Actions
向下滚动到“从这些发件人处获取通知”部分并确认“Google Chrome”(或您的首选浏览器)设置为“开启”
最近获取权限的 API 从接受回调更改为返回 Promise。 这样做的问题是,我们无法判断当前浏览器实现了哪个版本的 API,因此您必须同时实现并处理两者。
function askPermission() {
return new Promise(function(resolve, reject) {
const permissionResult = Notification.requestPermission(function(result) {
resolve(result);
});
if (permissionResult) {
permissionResult.then(resolve, reject);
}
})
.then(function(permissionResult) {
if (permissionResult !== 'granted') {
throw new Error('We weren\'t granted permission.');
}
});
}
这最初是由 Asaf 提出的,但我认为值得一提。
Windows 有一个“Focus Assist”,可以隐藏 Chrome 通知。 关闭它的完整指南在这里: https : //www.howtogeek.com/435349/how-to-disable-windows-10s-annoying-focus-assist-notifications/
快速的只是搜索“焦点辅助”,然后将其关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.