[英]Service worker notification click not working
I have to show notification on desktop and mobile browsers.我必须在桌面和移动浏览器上显示通知。 I tried to create functionality which is following.
我试图创建以下功能。
<script>
function registerServiceWorker() {
return navigator.serviceWorker.register('sw.js?v=12')
.then(function(registration) {
console.log('Service worker successfully registered.');
return registration;
})
.catch(function(err) {
console.error('Unable to register service worker.', err);
});
}
function displayNotification()
{
//alert(Notification.permission);
if(window.Notification && Notification.permission !== "denied") {
Notification.requestPermission(function(permission) {
navigator.serviceWorker.ready.then(function(reg)
{
console.log(reg);
var options = {
body: 'You have 1 new notification in Funafuti community.',
icon: '/images/icon.png',
vibrate: [100,50,100],
"badge": "/img/favicon-32x32.png",
data: { primaryKey:1 } ,
tag: "notificationid",
actions: [
{
action: 'check-action',
title: 'Check',
icon: '/images/popuparrow.png'
},
{
action: 'cancel-action',
title: 'Ignore',
icon: '/images/popuparrow.png'
}
]
};
var n=reg.showNotification('Notification',options);
});
});
}
}
displayNotification();
registerServiceWorker();
</script>
/ ---------------------------------------------------------------------- / sw.js code: / ------------------------------------------------- --------------------- / sw.js 代码:
<script>
self.addEventListener('notificationclick', function(event) {
if (!event.action) {
// Was a normal notification click
console.log('Notification Click.');
return;
}
switch (event.action) {
case 'check-action':
window.focus();
event.notification.close();
console.log('User coffee.');
break;
case 'cancel-action':
event.notification.close();
console.log('User doughnuts.');
break;
default:
console.log(`Unknown action clicked: '${event.action}'`);
break;
}
});
self.addEventListener('notificationclick', function(event) {
event.notification.close();
});
</script>
Notification is working fine in both mobile browser and desktop but after click on notification action nothing happen.通知在移动浏览器和桌面上都运行良好,但点击通知操作后什么也没有发生。
I am using addEventListener on notificationclick, but it's still not working.我在notificationclick 上使用addEventListener,但它仍然无法正常工作。
I was having the same problem.我遇到了同样的问题。 Can you check if you have focus assist on in your machine.
你能检查一下你的机器上是否有焦点辅助。 I had it on and it wasn't working.
我戴上了它,但它不起作用。 I switched it off and now it's working.
我把它关掉了,现在它可以工作了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.