--- title: NotificationEvent slug: Web/API/NotificationEvent translation_of: Web/API/NotificationEvent ---
{{APIRef("Web Notifications")}}
传递给 {{domxref("ServiceWorkerGlobalScope.onnotificationclick", "onnotificationclick")}} 处理程序的参数的NotificationEvent接口,该接口表示通知单击事件,该事件在 {{domxref("ServiceWorkerGlobalScope")}} ,{{domxref("ServiceWorker")}} 。
该接口继承自{{domxref("ExtendableEvent")}}接口。
NotificationEvent对象。从其祖先{{domxref("Event")}}继承属性。
从其父项{{domxref("ExtendableEvent")}}继承方法。
延长事件的寿命。告诉浏览器工作正在进行中。
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('/');
}));
});
| 规范 | 状态 | 评论 |
|---|---|---|
| {{SpecName('Web Notifications','#notificationevent','NotificationEvent')}} | {{Spec2('Web Notifications')}} | 生活水平。 |
注意:此接口在Notifications API中指定,但是可以通过 {{domxref("ServiceWorkerGlobalScope")}}访问。
{{Compat("api.NotificationEvent")}}