notificationclick
event is fired to indicate that a system notification spawned by
ServiceWorkerRegistration.showNotification()
has been clicked.
| 冒泡 | No |
|---|---|
| 可取消 | No |
| 接口 |
NotificationEvent
|
| Event handler |
onnotificationclick
|
可以使用
notificationclick
event in an
addEventListener
方法:
self.addEventListener('notificationclick', function(event) {
console.log('On notification click: ', event.notification.tag);
event.notification.close();
// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(clients.matchAll({
type: "window"
}).then(function(clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if (client.url == '/' && 'focus' in client)
return client.focus();
}
if (clients.openWindow)
return clients.openWindow('/');
}));
});
或使用
onnotificationclick
事件处理程序特性:
self.onnotificationclick = function(event) {
console.log('On notification click: ', event.notification.tag);
event.notification.close();
// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(clients.matchAll({
type: "window"
}).then(function(clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if (client.url == '/' && 'focus' in client)
return client.focus();
}
if (clients.openWindow)
return clients.openWindow('/');
}));
};
| 规范 | 状态 |
|---|---|
|
Notifications API
The definition of 'onnotificationclick' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
notificationclick
event
|
Chrome 40 | Edge ≤79 |
Firefox
44
|
IE No | Opera 24 | Safari 11.1 | WebView Android No | Chrome Android 40 | Firefox Android 44 | Opera Android 27 | Safari iOS 11.3 | Samsung Internet Android 4.0 |
完整支持
不支持
见实现注意事项。
ServiceWorkerGlobalScope
message
notificationclick