跳转到内容

通知 · API

通知触发器(showTrigger)

发布于

有限可用不支持的浏览器: Chrome (Desktop)说明文档

通知触发器是 Chrome 的一项提案,用于把持久通知安排到未来某个时刻显示:showNotification() 将接受一个持有 TimestampTrigger 的 showTrigger 选项,浏览器在该时刻显示通知,不需要打开页面,也不需要推送消息。Chrome 的文档声明开发已经终止,该特性仍留在实验性标志之后,其他引擎都没有实现,因此所有正式发布的浏览器里都没有这个选项。

registration.showNotification(title, { showTrigger: new TimestampTrigger(timestamp) })
registration.getNotifications({ includeTriggered: true })

两种形式只存在于开启了 chrome://flags/#enable-experimental-web-platform-features 的 Chromium 中(Notification Triggers,developer.chrome.com)。该特性在 Chrome Platform Status 上的状态为「In developer trial (Behind a flag)」(chromestatus.com);Chrome 为它运行过的 origin trial 之后没有正式发布。

参数 类型 含义
showTrigger TimestampTrigger 显示通知的时刻。构造函数接受以毫秒计的 DOMTimeStamp,可通过 trigger.timestamp 读回。过去的时间立即显示。
includeTriggered boolean,getNotifications() 的选项 把尚未显示的已安排通知也包含进来,以便页面列出或用 notification.close() 取消它们。默认 false。

提案还讨论过为被取消的触发器触发 notificationclose,以及对触发时刻能提前多远设限;两者都未写入规范。

无。

没有浏览器发布该选项。Chromium 以上述状态把实现留在实验性特性标志之后;Firefox 和 WebKit 没有实现,MDN 对 showNotification() 的参考列出 actions、badge、body、data、dir、icon、image、lang、navigate、renotify、requireInteraction、silent、tag、timestamp 与 vibrate,其中没有 showTrigger。公开的停止原因是无法跨平台提供一致、可靠的行为,这与限制周期性后台同步的是同一个约束。

前两个示例在开启标志后运行;第三个是唯一属于生产代码的。

开启标志后,触发器只是多出来的一个选项。tag 让后续调用替换已安排的通知,而不是再加一条。

async function scheduleReminder(registration, when, title) {
await registration.showNotification(title, {
tag: `reminder-${when}`,
body: '10 分钟后开始',
showTrigger: new TimestampTrigger(when - 10 * 60 * 1000),
});
}

调用时除了存下请求什么都不发生;即使浏览器在此期间被关闭,通知仍在触发时刻出现,这正是任何已发布 API 都不具备的特性。

getNotifications({ includeTriggered: true }) 把已安排的条目和已显示的一并返回;在触发之前关闭其中一条就是取消。

async function cancelReminder(registration, when) {
const pending = await registration.getNotifications({ includeTriggered: true });
for (const notification of pending) {
if (notification.tag === `reminder-${when}`) notification.close();
}
}

没有标志时选项对象被忽略,pending 只包含已显示的通知,循环找不到可取消的条目。

测试方法是 'showTrigger' in Notification.prototype,在所有正式发布的浏览器里都是 false。兜底是唯一的生产路径:把计划存在服务端,到时候用 Web Push 投递。

async function remindAt(registration, when, title) {
if ('showTrigger' in Notification.prototype) {
return registration.showNotification(title, { showTrigger: new TimestampTrigger(when) });
}
// 不开标志的浏览器都不支持:在服务端调度,用 Web Push 投递
await fetch('/api/reminders', { method: 'POST', body: JSON.stringify({ when, title }) });
}

服务端路径依赖投递时刻的网络连接,这正是提案想要消除的限制,也是它仍不时被人提起的原因。

规范

规范状态
Notification Triggers(通知触发器)说明文档
  • 图例
  • 支持
  • 部分支持
  • 需开启标志
  • 不支持
  • 未知
浏览器 / 平台支持版本置信度来源备注
Chrome (Desktop)不支持—中来源12
  1. 从未在 Chrome 中发布:Chrome Platform Status 显示,该特性在完成说明文档和两轮 origin trial(Chrome 80-83 与 86-88)后,发布里程碑仍为「Not started」;Chrome 自己的文档确认开发已结束。
  2. 本行来源于 Chrome 自己的状态页,仅证明 Chrome 的状态,不对其他浏览器引擎作任何断言。

源数据: /compatibility/notification-triggers.json · 全球使用占比: 0 % (StatCounter 2026-05)

来源: 规范 · MDN · 最近核验 2026-09-03 · 置信度: 中 (由来源计算)