我正在尝试从Chrome(Windows 10)中的通知获取通知点击事件。激活该服务后,它将自动显示带有两个按钮的通知消息。单击任何一个按钮都将触发通知单击处理程序,但不会。
这是一个简单的例子。
self.addEventListener('install', function (event) {
console.log(">>> installing client service worker");
self.skipWaiting();
});
self.addEventListener('activate', function (event) {
console.log('>>> client service activated');
showNotificationMessage("xxx", "yyy", "123");
});
self.addEventListener('notificationclick', function (event) {
console.log('>>> On notification click: ', event.notification.tag);
});
function showNotificationMessage(messageTitle, bodyMessage, data) {
registration.showNotification(messageTitle, {
actions: [{ action: "a", title: "action a" }, { action: "b", title: "action b", }],
body: bodyMessage,
requireInteraction: true,
renotify: true,
tag: 'test',
data: data
}).then(console.log('>>> notification was successful')).catch(function (error) {
console.log(">>> showNotification error: " + error);
});
}
<!DOCTYPE html>
<html>
<head>
<script>
if ('serviceWorker' in navigator) {
// Register service worker
navigator.serviceWorker.register('Client.js').then(function (reg) {
console.log("SW registration succeeded. Scope is " + reg.scope);
}).catch(function (err) {
console.error("SW registration failed with error " + err);
});
}
</script>
</head>
<body>
When this page loads, a notification should be displayed from the service worker. Clicking the action buttons should cause a notification click event in the service worker, but it doesn't.
</body>
</html>