NotificationEvent - 表示通知点击事件
NotificationEvent 是传递给 onnotificationclick 处理程序的参数,表示在 ServiceWorker 的 ServiceWorkerGlobalScope 上调度的通知单击事件
该接口继承自 ExtendableEvent 接口。
构造函数
NotificationEvent.NotificationEvent()
创建一个新的 NotificationEvent 对象。
属性
继承其祖先 Event 的属性。
NotificationEvent.notification 只读
返回一个 Notification 对象,该对象表示单击以触发事件的通知。
NotificationEvent.action 只读
返回用户单击的通知按钮的字符串 ID。如果用户在除操作按钮之外的其他位置单击了通知,或者该通知没有按钮,则此值返回一个空字符串。
方法
从其父接口 ExtendableEvent 继承了方法。
ExtendableEvent.waitUntil()
延长事件的时间。它用于在 installing worker 的 install} EventHandler} 中,以及 active worker 的 active EventHandler 中调用。
实例
self.addEventListener('notificationclick', function(event) {
console.log('点击通知:', event.notification.tag);
event.notification.close();
// 查看当前是否已打开,如果当前已打开,则进行聚焦
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 NotificationEvent 的定义 |
现行的标准 | Living standard. |
注意:此接口在 Notifications API 中指定,但是通过
ServiceWorkerGlobalScope访问。
桌面浏览器兼容性
| 特性 | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|---|
| feature_basicsupport | 42 | 支持 | 441 | 未知 | 37 | 未知 |
NotificationEvent() constructor | 42 | 支持 | 441 | 未知 | 37 | 未知 |
action | 48 | 支持 | 441 | 未知 | 不支持 | 未知 |
notification | 42 | 支持 | 441 | 未知 | 37 | 未知 |
移动浏览器兼容性
| 特性 | Android | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
|---|---|---|---|---|---|---|---|
| feature_basicsupport | 不支持 | 42 | 未知 | 44 | 未知 | 37 | 未知 |
NotificationEvent() constructor | 不支持 | 42 | 未知 | 44 | 未知 | 37 | 未知 |
action | 不支持 | 48 | 未知 | 44 | 未知 | 不支持 | 未知 |
notification | 不支持 | 42 | 未知 | 44 | 未知 | 37 | 未知 |
1. 在 Firefox 45 和 52 扩展支持版本(ESR)中,服务 worker(和 Push)已被禁用。