# Notifications API

> 用 showNotification() 显示系统通知、用 getNotifications() 读取现有通知、持久与非持久通知之分、各引擎支持程度不同的选项，以及 TypeError 路径。

Notifications API 通过操作系统自己的通知界面在页面之外显示消息，用户切换标签页或应用后消息仍然可见。`ServiceWorkerRegistration.showNotification()` 创建持久通知，页面关闭后仍能保留，MDN 指出，在大多数移动浏览器上 `Notification()` 构造函数会抛出 `TypeError`，因此需要在移动设备上运行的代码必须使用持久通知。页面也能用 `new Notification()` 创建非持久通知，用法见[构造函数参考](/zh/reference/notifications/notification/)。

## 语法

```js
registration.showNotification(title)
registration.showNotification(title, options)
registration.getNotifications()
registration.getNotifications({ tag })
```

`showNotification()` 返回 `Promise<void>`，通知交给操作系统后兑现；`getNotifications()` 以该注册已显示的 `Notification` 对象兑现，按创建顺序排列，可按 `tag` 过滤。两者在窗口和 Service Worker 自身（`self.registration`）中都可用，且要求安全上下文（BCD `api.ServiceWorkerRegistration.showNotification`：Chrome 42、Firefox 44、macOS Ventura 上的 Safari 16、iOS 上的 Safari 16.4 仅限主屏幕 Web App）。

## 参数

| 参数 | 类型 | 含义 |
|---|---|---|
| `title` | string | 通知的第一行。必填。 |
| `options.body` | string | 标题下方的正文。 |
| `options.icon` | string（URL） | 通知图标，例如应用标志或发送者头像。大小和位置取决于浏览器及操作系统。 |
| `options.badge` | string（URL） | 完整通知放不下时使用的小型单色图，例如 Android 状态栏。仅 Chromium；见 [actions 与 badge](/zh/reference/notifications/notification-actions-badge/)。 |
| `options.image` | string（URL） | 通知内容图片，与 `icon` 不同；展开的通知可能显示它，具体布局取决于平台。仅 Chromium。 |
| `options.tag` | string | 标识符；相同 tag 的新通知替换旧通知。 |
| `options.renotify` | boolean | 按 `tag` 替换时再次提醒用户。要求非空 `tag`。仅 Chromium。 |
| `options.requireInteraction` | boolean | 通知保留在屏幕上直到用户操作。Chromium；Firefox 117 仅限 Windows。 |
| `options.silent` | boolean | 不发声、不振动。Chrome 43、Firefox 132、Safari 16.6。与 `vibrate` 不兼容。 |
| `options.data` | 任意可结构化克隆的值 | 在 `notificationclick` 中以 `event.notification.data` 读回的载荷。 |
| `options.actions` | array | 按钮；仅持久通知。见 actions 条目。 |
| `options.navigate` | string（URL） | Safari 18.4：激活时打开该 URL，而不触发 `notificationclick`。 |
| `options.timestamp`、`options.vibrate`、`options.dir`、`options.lang` | number、array、string、string | 显示时间、振动模式（Android 上的 Chromium）、文字方向、BCP 47 语言标签。 |

持久通知拥有非空的 Service Worker 注册。它的 `notificationclick` 与 `notificationclose` 事件在 `ServiceWorkerGlobalScope` 上触发，它应当出现在平台的通知中心，并且可以携带 `actions`。非持久通知在其 `Notification` 对象上触发 `click` 与 `close`，按规范浏览器应在创建几秒后将其关闭，其构造函数遇到非空的 `actions` 数组会抛出异常。

## 异常

| 异常 | 触发条件 |
|---|---|
| `TypeError` | 注册没有处于 `activating` 或 `activated` 状态的 worker。`navigator.serviceWorker.ready` 只在存在这样的 worker 时兑现，示例等待它正是为此。 |
| `TypeError` | `Notification.permission` 不是 `"granted"`。Chromium 的报错文本为 `Failed to execute 'showNotification' on 'ServiceWorkerRegistration': No notification permission has been granted for this origin.`（[service_worker_registration_notifications.cc](https://chromium.googlesource.com/chromium/src/+/main/third_party/blink/renderer/modules/notifications/service_worker_registration_notifications.cc)，chromium.googlesource.com）。 |
| `TypeError` | `renotify` 为 `true` 但 `tag` 为空，或 `silent` 为 `true` 同时设置了 `vibrate`。 |
| `DataCloneError` `DOMException` | `options.data` 无法结构化克隆（函数、DOM 节点）。 |

调用会拒绝 Promise，不会同步抛出异常。在 `push` 处理器中，`showNotification()` 拒绝还会违反 `userVisibleOnly` 的承诺，导致 WebKit 撤销订阅。所以要在调用前处理这些情况，而不是放在 `catch` 里。

## 各引擎的支持情况

桌面端支持完整：Chrome 42、Edge 17、Firefox 44、macOS Ventura 上的 Safari 16。移动端才是形态真正起作用的地方。Chrome for Android（42）与 Samsung Internet 只通过 Service Worker 暴露通知，并让构造函数抛出；iOS 与 iPadOS 上的 Safari 16.4 只在主屏幕 Web App 内暴露该接口，而且只能通过 Service Worker；Android WebView 没有实现（BCD `api.Notification`）。Chrome 自 49 起还在无痕窗口中禁用通知。要同时支持桌面端和移动端，应使用持久通知。

## 示例

页面负责显示通知，Service Worker 负责响应点击。

### 从页面显示持久通知

等待活跃的 worker，然后在注册上调用 `showNotification()`。`data` 成员携带点击处理函数需要的内容。

```js
async function notify(title, body, url) {
  const registration = await navigator.serviceWorker.ready;
  await registration.showNotification(title, {
    body,
    icon: '/icons/icon-192.png',
    tag: 'inbox',
    data: { url },
  });
}
```

`tag: 'inbox'` 把重复调用合并成一条通知，这是「3 条新消息」想要的效果，却不是三个独立聊天线程想要的。

### 在 Service Worker 中处理点击

持久通知在 worker 上触发 `notificationclick`。先关闭通知，然后若目标 URL 已有打开的窗口就聚焦它，否则打开新窗口。

```js
self.addEventListener('notificationclick', (event) => {
  event.notification.close();
  const url = new URL(event.notification.data?.url ?? '/', self.location.origin).href;
  event.waitUntil((async () => {
    const windows = await self.clients.matchAll({ type: 'window', includeUncontrolled: true });
    const open = windows.find((client) => client.url === url);
    if (open) return open.focus();
    return self.clients.openWindow(url);
  })());
});
```

点击通知主体时 `event.action` 为空字符串，点击按钮时为该按钮的 `action` id；上面的处理函数对两者一视同仁。

### 检测可用的通知器并兜底

先检查接口是否存在、权限是否已授予、注册是否有活跃 worker。任一条件不满足就返回页内兜底。没有注册的页面若等待 `ready`，Promise 会一直挂起。

```js
async function getNotifier() {
  if (!('Notification' in window) || !('serviceWorker' in navigator)) {
    return showInPageBanner; // iOS 标签页或应用内嵌网页视图：没有系统通知
  }
  const registration = await navigator.serviceWorker.getRegistration();
  if (!registration?.active || Notification.permission !== 'granted') {
    return showInPageBanner; // 尚未安装，或权限未授予
  }
  return (title, body) => registration.showNotification(title, { body });
}
```

两个分支返回的函数签名相同，调用方不必关心拿到的是哪一个。

:::caution
只要通知权限不是 `"granted"`，`registration.showNotification()` 就会以 `TypeError` 拒绝，所以 `"default"` 与 `"denied"` 都会失败，信息为 `No notification permission has been granted for this origin.`；这项检查在 Chromium 的 [service_worker_registration_notifications.cc](https://chromium.googlesource.com/chromium/src/+/main/third_party/blink/renderer/modules/notifications/service_worker_registration_notifications.cc)（chromium.googlesource.com）中。同一函数在注册没有 active worker 时以 `No active registration available on the ServiceWorkerRegistration.` 拒绝。`renotify` 缺少 `tag`、`silent` 与 `vibrate` 同时出现的错误来自同目录的 [notification_data.cc](https://chromium.googlesource.com/chromium/src/+/main/third_party/blink/renderer/modules/notifications/notification_data.cc)。Chrome DevTools 可以在 Application > Background services > Notifications 下记录通知事件（[Debug background services](https://developer.chrome.com/docs/devtools/javascript/background-services)，developer.chrome.com）。
:::

## 另请参阅

- [Notifications API Standard](https://notifications.spec.whatwg.org/)（whatwg.org）
- [ServiceWorkerRegistration: showNotification() method](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification)（developer.mozilla.org）
- [Notification 接口](/zh/reference/notifications/notification/)
- [Notification.requestPermission()](/zh/reference/notifications/permissions/)
- [Web Push 与 PushManager.subscribe()](/zh/reference/notifications/web-push/)
- [通知的 actions 与 badge 选项](/zh/reference/notifications/notification-actions-badge/)