NotificationEvent:notification 屬性

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

注意:此功能僅在 Service Workers 中可用。

NotificationEvent 介面的notification 只讀屬性返回在觸發事件時被點選的 Notification 例項。該 Notification 提供了對許多屬性的只讀訪問,這些屬性是在 Notification 例項化時設定的,例如 tagdata 屬性,允許你在 notificationclick 事件中儲存資訊以供後續使用。

一個 Notification 物件。

示例

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

  // Data can be attached to the notification so that you
  // can process it in the notificationclick handler.
  console.log(`Notification Tag: ${event.notification.tag}`);
  console.log(`Notification Data: ${event.notification.data}`);
  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((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

瀏覽器相容性