大网数据广告活动
Web 标准与前端开发

NotificationEvent

· 52CE 我爱测

Notifications API 的 **`NotificationEvent`** 接口表示在 ServiceWorker 的 ServiceWorkerGlobalScope 上触发的通知事件。

Web Notificationsservice

Notifications API 的 NotificationEvent 接口表示在 ServiceWorker 的 ServiceWorkerGlobalScope 上触发的通知事件。

该接口继承自 ExtendableEvent 接口。

[!NOTE] 只有在 ServiceWorkerGlobalScope 对象上触发的持久通知事件才会使用 NotificationEvent 接口。非持久通知事件在 Notification 对象上触发,使用 Event 接口。

构造函数

  • NotificationEvent.NotificationEvent
    • : 创建一个新的 NotificationEvent 对象。

实例属性

还从其父接口 ExtendableEvent 继承属性。

  • NotificationEvent.notification
    • : 返回一个 Notification 对象,表示单击以触发事件的通知。
  • NotificationEvent.action
    • : 返回用户单击的通知按钮的字符串 ID。如果用户单击操作按钮以外的通知,或者通知没有按钮,则此值返回空字符串。

实例方法

还从其父接口 ExtendableEvent 继承方法。

示例

self.addEventListener("notificationclick", (event) => {
  console.log(`通知被点击:${event.notification.tag}`);
  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("/");
      }),
  );
});

规范

[!NOTE] 此接口在 Notifications API 中指定,但是可以通过 ServiceWorkerGlobalScope 访问。

浏览器兼容性

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。