NotificationEvent:notification 属性

Limited availability

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

备注: 此特性仅在 Service Worker 中可用。

NotificationEvent 接口的 notification 只读属性返回被单击而触发事件的 Notification 实例。Notification 提供对在通知实例化时设置的许多属性的只读访问权限,例如 tagdata 属性,这些属性允许你在通知的 notificationclick 事件中延迟使用存储的信息。

一个 Notification 对象。

示例

js
self.addEventListener("notificationclick", (event) => {
  console.log("通知被点击");

  // 数据可以附加到通知中,以便你在 notificationclick 事件处理方法中对其进行处理。
  console.log(`Notification Tag: ${event.notification.tag}`);
  console.log(`Notification Data: ${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