Service worker通知承诺被破坏
Service worker notification promise broken?
从MDN中我看到showNotification
返回一个承诺,应该解析为NotificationEvent
。
<标题> 语法ServiceWorkerRegistration.showNotification(标题、).then(function(NotificationEvent){…});
返回解析为NotificationEvent的Promise。
标题>
然而,我在这里设置了它,通知正在发送,但如果你看一下控制台,你会注意到event
是未定义的。
navigator.serviceWorker.register('worker.js');
Notification.requestPermission(function (result) {
if (result === 'granted') {
navigator.serviceWorker.ready.then(function (registration) {
registration.showNotification('Laff', {
body: 'Hello, you have unread mesages!',
icon: '/apple-touch-icon.png',
tag: 'test'
}).then(function(event){
console.log(event);
});
});
}
});
我需要获得通知,我认为我可以从event.notification
做,但由于event
是未定义的,我真的不知道该怎么做。
我做错了什么吗?
我不知道你说的get hold on notification是什么意思?如果您希望在用户单击通知时捕获事件,则可以通过添加侦听器来实现。
add return。
return registration.showNotification('Laff', {
捕捉notificationClick上的事件:
self.addEventListener('notificationclick', function (event) {
var tag = event;
}
希望有所帮助
文档中确实有一个问题,showNotification
方法没有返回NotificationEvent
对象。
如果您不想等待点击,您可以使用getNotifications
方法。下面是一个代码,用于在3秒后关闭通知。
self.registration.showNotification(...).then(() => {
self.registration.getNotifications({tag: ...}).then(notifications => {
setTimeout(() => notifications[0].close(), 3000);
});
}
})
更多信息在这里:https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/getNotifications
相关文章:
- 我的职位回报太快了,如何做出承诺
- 打破承诺链的好方法是什么
- 从函数返回角度承诺
- 在同一个服务工作者中处理service-worker.js有任何影响吗
- 我怎样才能获得承诺的价值
- 延期承诺值未更新/解析/延期
- 在承诺链中处理早期回报的最佳方式
- 承诺在非节点式回调上使用Bluebird
- 简单的ES6承诺问题-交换解决和拒绝参数
- 组合承诺和非承诺值
- 带有对象/原型的链式承诺(Q延期)
- AngularJS$q承诺使用socket.io
- React JS:未捕获(在承诺中)语法错误:在位置 0 的 JSON 中意外<令牌
- 当一些承诺失败时,如何继续使用$q.all()
- Nodejs和express路由,如何处理客户端的承诺
- 如何在多承诺链中处理谷歌地图API V3事件
- 承诺合并流
- JavaScript承诺-无法读取属性'那么'的未定义
- 如何通过worker.onerror在承诺中冒泡一个web worker错误
- Service worker通知承诺被破坏