传递给 onnotificationclick
处理程序的参数的NotificationEvent
接口,该接口表示通知单击事件,该事件在 ServiceWorkerGlobalScope
,ServiceWorker
。
该接口继承自ExtendableEvent
接口。
建设者
NotificationEvent()
- 创建一个新
NotificationEvent
对象。
物产
从其祖先Event
继承属性。
NotificationEvent.notification
只读- 返回一个
Notification
对象,该对象表示单击以触发事件的通知。 NotificationEvent.action
只读- 返回用户单击的通知按钮的字符串ID。如果用户在除操作按钮之外的其他位置单击了通知,或者该通知没有按钮,则此值返回一个空字符串。
方法
从其父项ExtendableEvent
继承方法。
ExtendableEvent.waitUntil()
-
延长事件的寿命。告诉浏览器工作正在进行中。
例
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('/');
}));
});
技术指标
规范 | 状态 | 评论 |
---|---|---|
Notifications API NotificationEvent |
Living Standard | 生活水平。 |
注意:此接口在Notifications API中指定,但是可以通过 ServiceWorkerGlobalScope
访问。
浏览器兼容性
BCD tables only load in the browser
此页面上的兼容性表是根据结构化数据生成的。如果您想贡献数据,请查看https://github.com/mdn/browser-compat-data并向我们发送请求请求。