--- title: NotificationEvent slug: Web/API/NotificationEvent tags: - API - Interface - NotificationEvent - Reference - Service Workers - ServiceWorker translation_of: Web/API/NotificationEvent ---
{{APIRef("Web Notifications")}}
{{domxref("ServiceWorkerGlobalScope.onnotificationclick", "onnotificationclick")}} ハンドラーに渡される引数である NotificationEvent インターフェイスは、 {{domxref("ServiceWorker")}} の {{domxref("ServiceWorkerGlobalScope")}} で配信される通知クリックイベントを表します。
このインターフェイスは {{domxref("ExtendableEvent")}} インターフェイスを継承しています。
NotificationEvent オブジェクトを作成します。祖先である {{domxref("Event")}} からプロパティを継承しています。
親の {{domxref("ExtendableEvent")}} からメソッドを継承しています。
イベントの存続期間を延長します。作業が進行中であることをブラウザーに伝えます。
self.addEventListener('notificationclick', function(event) {
console.log('On notification click: ', 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('/');
}));
});
| 仕様書 | 状態 | 備考 |
|---|---|---|
| {{SpecName('Web Notifications','#notificationevent','NotificationEvent')}} | {{Spec2('Web Notifications')}} | Living standard. |
注: このインターフェイスは Notifications API で定義されていますが、{{domxref("ServiceWorkerGlobalScope")}} を介してアクセスします。
{{Compat("api.NotificationEvent")}}