NotificationEvent:notification 属性

注意:此功能仅在服务工作线程中可用。

notification只读属性NotificationEvent接口返回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("/");
      }),
  );
});

规范

规范
通知 API 标准
# ref-for-dom-notificationevent-notification

浏览器兼容性

BCD 表格仅在浏览器中加载