NotificationEvent: notification プロパティ
利用可能性は限定的
この機能はベースラインではありません。最も広く使用されているブラウザーの一部で動作しません。
もっと多くのブラウザーがこの機能に対応してほしいですか? 理由を教えてください。
メモ: この機能はサービスワーカー内でのみ利用可能です。
notification は NotificationEvent インターフェイスの読み取り専用プロパティで、クリックされてイベントを発生させた Notification のインスタンスを返します。Notification は tag や data 属性など、Notification のインスタンス化時に設定された多くのプロパティへの読み取り専用アクセスを提供し、あとで notificationclick イベントで使用するための情報を保存できます。
返値
Notification オブジェクト。
例
js
self.addEventListener("notificationclick", (event) => {
console.log("通知クリック時");
// 通知にデータを添付して、`notificationclick` ハンドラーで
// 処理できる
console.log(`通知タグ: ${event.notification.tag}`);
console.log(`通知データ: ${event.notification.data}`);
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("/");
}),
);
});
仕様書
| 仕様書 |
|---|
| Notifications API> # dom-notificationevent-notification> |