NotificationEvent: notification プロパティ

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

メモ: この機能はサービスワーカー内でのみ利用可能です。

notificationNotificationEvent インターフェイスの読み取り専用プロパティで、クリックされてイベントを発行した Notification のインスタンスを返します。 Notificationtagdata 属性など、 Notification のインスタンス化時に設定された多くのプロパティへの読み取り専用アクセスを提供しており、あとで notificationclick イベントで使用するための情報を保存することができます。

返値

Notification オブジェクト。

js
self.addEventListener("notificationclick", (event) => {
  console.log("On notification click");

  // 通知にデータを添付することで、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("/");
      }),
  );
});

仕様書

Specification
Notifications API
# ref-for-dom-notificationevent-notification

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
notification

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support