NotificationEvent
Baseline Widely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2023年3月.
* Some parts of this feature may have varying levels of support.
メモ: この機能はサービスワーカー内でのみ利用可能です。
NotificationEvent は通知 APIのインターフェイスで、サービスワーカー (ServiceWorker) の ServiceWorkerGlobalScope で配信される通知イベントを表します。
このインターフェイスは ExtendableEvent インターフェイスを継承しています。
メモ: ServiceWorkerGlobalScope オブジェクトで発行される持続的な通知イベントのみが、NotificationEventインターフェイスを実装します。Notification オブジェクトで発行される非持続的な通知イベントは、Eventインターフェイスを実装します。
コンストラクター
NotificationEvent()-
新しい
NotificationEventオブジェクトを作成します。
インスタンスプロパティ
親インターフェイスである ExtendableEvent から継承したプロパティもあります。
NotificationEvent.notification読取専用-
イベントを発生させるためにクリックされた通知を表す
Notificationオブジェクトを返します。 NotificationEvent.action読取専用-
ユーザーがクリックした通知ボタンの文字列 ID を返します。ユーザーがアクションボタン以外の場所で通知をクリックした場合、または通知にボタンがない場合、この値は空の文字列を返します。
インスタンスメソッド
親インターフェイスである ExtendableEvent から継承したメソッドもあります。
例
self.addEventListener("notificationclick", (event) => { console.log(`On notification click: ${event.notification.tag}`); event.notification.close(); // これは、現在のページが既に開いているかどうかを確認し、 // そうならばフォーカスします event.waitUntil( clients .matchAll({ type: "window", }) .then((clientList) => { for (const client of clientList) { if (client.url === "/" && "focus" in client) return client.focus(); } if (clients.openWindow) return clients.openWindow("/"); }), ); }); 仕様書
| Specification |
|---|
| Notifications API> # notificationevent> |
メモ: このインターフェイスは通知 API で定義されていますが、ServiceWorkerGlobalScope を通してアクセスします。