通知 API 标准

现行标准 — 最后更新

参与:
GitHub whatwg/notifications (新建议题, 未解决议题)
Matrix 聊天
提交:
GitHub whatwg/notifications/commits
当前提交的快照
@notifyapi
测试:
web-platform-tests notifications/ (进行中的工作)
翻译 (非规范性)
日本語
简体中文
한국어

摘要

本标准定义了一个向终端用户显示通知的 API,通常在顶级浏览上下文视口之外显示。它设计为与现有通知系统兼容,同时保持平台无关性。

1. 术语

本规范依赖于 Infra 标准。[INFRA]

本规范中使用的一些术语在 DOM、Fetch、高精度时间、HTML、IDL、Service Workers、URL 和振动 API 标准中定义。 [DOM] [FETCH] [HR-TIME] [HTML] [WEBIDL] [SERVICE-WORKERS] [URL] [VIBRATION]

2. 通知

通知是已发生事件的抽象表示,例如消息的传递。

通知有一个关联的 service worker 注册(null 或一个 service worker 注册)。初始值为 null。

通知 有一个关联的 标题(一个字符串)。

通知 有一个关联的 方向("auto"、"ltr" 或 "rtl")。

通知 有一个关联的 语言(一个字符串)。

通知 有一个关联的 正文(一个字符串)。

通知 有一个关联的 导航 URL (null 或一个 URL)。初始值为 null。

通知 有一个关联的 标签(一个字符串)。

通知 有一个关联的 数据(一个 Record)。

通知 有一个关联的 时间戳 (一个 EpochTimeStamp)。

时间戳可用于指示通知的实际时间。例如,当设备离线而无法立即传递消息时,这可能是过去的时间,或者对于即将开始的会议,这可能是未来的时间。

通知 有一个关联的 (一个 )。

通知 有一个关联的 重新通知偏好(一个布尔值)。初始值为 false。当为 true 时,表示在运行了通知显示步骤后,如果新通知与现有通知具有相同的标签,应该提醒终端用户。

通知 有一个关联的 静音偏好(null 或一个布尔值)。初始值为 null。当为 true 时,表示不应产生声音或振动。当为 null 时,表示产生声音或振动应遵循平台约定。

通知 有一个关联的 需要交互偏好 (一个布尔值)。初始值为 false。当为 true 时,表示在具有足够大屏幕的设备上,通知应保持可用状态,直到终端用户激活或关闭通知。

通知 可以具有这些关联的图形:一个 图像 URL图标 URL徽章 URL;以及它们对应的 图像资源图标资源徽章资源

图像资源是作为通知内容一部分显示的图片,应该比图标资源徽章资源具有更高的视觉优先级,尽管它可能在较少的情况下显示。

图标资源是增强通知的图像(例如图标或发送者的照片)。

徽章资源是代表 Web 应用程序的图标,或者如果 Web 应用程序发送各种各样的通知,则代表通知的类别。当没有足够空间显示通知本身时,可以用它来表示通知。它也可以通知内部显示,但那时它应该比图像资源图标资源具有较低的视觉优先级。

通知 有一个关联的 振动模式(一个 列表)。初始值为 « »。

鼓励开发者不要通过图像图标徽章振动模式传达终端用户无法以其他方式访问的信息,特别是因为不支持这些功能的通知平台可能会忽略它们。

通知 有关联的 操作 (零个或多个通知操作列表)。通知操作代表终端用户的选择。每个通知操作都有一个关联的:

名称
一个字符串。
标题
一个字符串。
导航 URL
Null 或一个 URL。初始值为 null。
图标 URL
Null 或一个 URL。初始值为 null。
图标资源
Null 或一个资源。初始值为 null。

用户可以激活操作,作为激活通知本身的替代方案。支持的最大操作数量是一个实现定义的零或更多的整数,在通知平台的约束范围内。

由于操作的显示取决于平台,鼓励开发者确保终端用户可以从通知中调用的任何操作在 Web 应用程序中也可用。

某些平台可能会在向终端用户显示之前修改图标资源以更好地匹配平台的视觉风格,例如通过圆化边角或用特定颜色绘制。鼓励开发者使用能够优雅处理此类情况并且不会因为例如颜色丢失或边角被裁剪而丢失重要信息的图标。

非持久通知是其通知service worker 注册为 null 的通知。

(这是一个跟踪向量。) 持久通知是其通知service worker 注册非 null 的通知。


使用设置对象创建通知,给定字符串 titleNotificationOptions 字典 options环境设置对象 settings

  1. originsettings

  2. baseURLsettingsAPI 基础 URL

  3. fallbackTimestamp 为从 Unix 纪元settings当前挂钟时间的毫秒数,舍入到最接近的整数。

  4. 返回创建通知给定 titleoptionsoriginbaseURLfallbackTimestamp 的结果。

创建通知,给定字符串 titleNotificationOptions 字典 options originURL baseURLEpochTimeStamp fallbackTimestamp

  1. notification 为新的通知

  2. 如果 options["silent"] 为 true 并且 options["vibrate"] 存在,则抛出 TypeError

  3. 如果 options["renotify"] 为 true 并且 options["tag"] 为空字符串,则抛出 TypeError

  4. notification数据设置为 StructuredSerializeForStorage(options["data"])。

  5. notification标题设置为 title

  6. notification方向设置为 options["dir"]。

  7. notification语言设置为 options["lang"]。

  8. notification设置为 origin

  9. notification正文设置为 options["body"]。

  10. 如果 options["navigate"] 存在,则使用 baseURL 解析它,如果解析不返回失败,则将 notification导航 URL 设置为返回值。(否则 notification导航 URL 保持为 null。)

  11. notification标签设置为 options["tag"]。

  12. 如果 options["image"] 存在,则使用 baseURL 解析它,如果解析不返回失败,则将 notification图像 URL 设置为返回值。(否则 notification图像 URL 不设置。)

  13. 如果 options["icon"] 存在,则使用 baseURL 解析它,如果解析不返回失败,则将 notification图标 URL 设置为返回值。(否则 notification图标 URL 不设置。)

  14. 如果 options["badge"] 存在,则使用 baseURL 解析它,如果解析不返回失败,则将 notification徽章 URL 设置为返回值。(否则 notification徽章 URL 不设置。)

  15. 如果 options["vibrate"] 存在,则 验证和规范化它,并将 notification振动模式设置为返回值。

  16. 如果 options["timestamp"] 存在,则将 notification时间戳设置为该值. 否则,将 notification时间戳设置为 fallbackTimestamp

  17. notification重新通知偏好设置为 options["renotify"]。

  18. notification静音偏好设置为 options["silent"]。

  19. notification需要交互偏好设置为 options["requireInteraction"]。

  20. notification操作设置为 « »。

  21. 对于 options["actions"] 中的每个 entry,直到支持的 最大操作数量(跳过任何多余的条目):

    1. action 为新的通知操作

    2. action名称设置为 entry["action"]。

    3. action标题设置为 entry["title"]。

    4. 如果 entry["navigate"] 存在,则使用 baseURL 解析它,如果解析不返回失败,则将 action导航 URL 设置为返回值。 (否则 action导航 URL 保持为 null。)

    5. 如果 entry["icon"] 存在,则使用 baseURL 解析它,如果解析不返回失败,则将 action图标 URL 设置为返回值。(否则 action图标 URL 保持为 null。)

    6. action 追加到 notification操作中。

  22. 返回 notification

2.1. 生命周期和用户界面集成

用户代理必须维护一个 通知列表,它是一个 列表,包含零个或多个 通知

用户代理应在 非持久通知 创建后几秒钟运行 关闭步骤

用户代理不应在平台的“通知中心”(如有)显示 非持久通知

用户代理应在 持久通知通知列表移除前一直保留它们。

可以通过调用其 close() 方法关闭某个 持久通知Notification 对象。

用户代理应在平台的“通知中心”(如有)显示 持久通知

2.2. 权限集成

通知 API 是一个 强大功能,通过 名称 "notifications" 进行标识。 [Permissions]

获取通知权限状态,请运行以下步骤:

  1. permissionState 为使用 "notifications" 获取当前权限状态 的结果。

  2. 如果 permissionState 为 "prompt",则返回 "default"。

  3. 返回 permissionState

2.3. 方向

本节采用与 HTML 渲染部分等效的术语编写。[HTML]

用户代理应遵循通知 通知标题正文以及每个 操作标题的 Unicode 语义。每个都应在显示时作为双向算法规则 P1、P2、P3 定义的一个或多个独立段落处理,包括例如支持 U+000A 换行符的段落分割行为。对于 标题正文和每个 操作标题的每个段落,如果 通知方向 不为 "auto",则它提供了对规则 P2 和 P3 的高层覆盖。[BIDI]

通知方向还决定了 通知操作在通知平台并排显示时向终端用户展示的相对顺序。

2.4. 语言

通知语言指定了 通知标题正文以及每个 操作标题的主要语言。其值为字符串。空字符串表示主要语言未知。其他字符串必须被解释为语言标签。不会强制有效性或格式正确。[BCP47]

鼓励开发者仅使用有效的语言标签。

2.5. 资源

获取步骤,用于给定 通知 notification

  1. 如果通知平台支持图片,则 获取 notification图片 URL(如果已设置)。

    意图是类似于 <img> 的方式获取该资源,但这 需要抽象

    然后,并行

    1. 等待 响应

    2. 如果 响应内部响应类型为 "default",则尝试将资源解码为图片。

    3. 如果图片格式受支持,则将 notification图片资源 设置为解码后的资源。(否则 notification 没有 图片资源。)

  2. 如果通知平台支持图标,则 获取 notification图标 URL(如果已设置)。

    意图是类似于 <img> 的方式获取该资源,但这 需要抽象

    然后,并行

    1. 等待 响应

    2. 如果 响应内部响应类型为 "default",则尝试将资源解码为图片。

    3. 如果图片格式受支持,则将 notification图标资源 设置为解码后的资源。(否则 notification 没有 图标资源。)

  3. 如果通知平台支持徽章,则 获取 notification徽章 URL(如果已设置)。

    意图是类似于 <img> 的方式获取该资源,但这 需要抽象

    然后,并行

    1. 等待 响应

    2. 如果 响应内部响应类型为 "default",则尝试将资源解码为图片。

    3. 如果图片格式受支持,则将 notification徽章资源 设置为解码后的资源。(否则 notification 没有 徽章资源。)

  4. 如果通知平台支持操作和操作图标,则对于 notification 的每个 action:如果 图标 URL 非空,则 获取 action图标 URL

    意图是类似于 <img> 的方式获取该资源,但这 需要抽象

    然后,并行

    1. 等待 响应

    2. 如果 响应内部响应类型为 "default",则尝试将资源解码为图片。

    3. 如果图片格式受支持,则将 action图标资源 设置为解码后的资源。(否则 action图标资源 保持为 null。)

2.6. 显示通知

通知显示步骤,用于给定的通知 notification

  1. notification 运行获取步骤

  2. 等待任何获取完成,以及 notification图像资源图标资源徽章资源 被设置(如果有),以及 notification操作图标 资源(如果有)。

  3. shown 为 false。

  4. oldNotification通知列表中的通知,其标签不是空字符串且与 notification标签相同, 并且其notification同源(如果有),否则为 null。

  5. 如果 oldNotification 非 null,则:

    1. oldNotification 处理关闭事件

    2. 如果通知平台支持替换,则:

      1. 通知列表中,用 notification 替换 oldNotification

      2. shown 设置为 true。

      强烈鼓励通知平台支持原生 替换,因为这会带来更好的用户体验。

    3. 否则,从通知列表移除 oldNotification

  6. 如果 shown 为 false,则:

    1. notification 附加通知列表

    2. 在设备上显示 notification(例如,通过调用适当的通知平台 API)。

  7. 如果 shown 为 false 或 oldNotification 非 null,并且 notification重新通知偏好为 true,则为 notification 运行提醒步骤

  8. 如果 notification非持久通知,则队列任务以在代表 notificationNotification 对象上触发事件,事件名为 show

2.7. 激活通知

通知 notification 或其 操作之一被终端用户激活时, 假设底层通知平台支持激活,用户代理必须(除非另有规定)运行以下步骤:

  1. action 为 null。

  2. 如果 notification操作之一被终端用户激活, 则将 action 设置为该通知操作

  3. navigationURLnotification导航 URL

  4. 如果 action 非 null,则将 navigationURL 设置为 action导航 URL

    这故意使得当通知操作导航 URL 为 null 时,它会落到 click 事件,为 Web 开发者提供更多灵活性。

  5. 如果 navigationURL 非 null:

    1. 实现定义的方式从以下两个选项中选择一个:

      强烈鼓励用户代理匹配平台约定。

    2. 返回。

  6. 如果 notification持久通知,则:

    1. actionName 为空字符串。

    2. 如果 action 非 null,则将 actionName 设置为 action名称

    3. 给定 notificationactionName触发 Service Worker 通知 事件,事件名为 "notificationclick"。
  7. 否则,队列任务以运行以下步骤:

    1. intoFocus 为在代表 notificationNotification 对象上触发事件的结果,事件名为 click,其 cancelable 属性初始化为 true。

      鼓励用户代理使 focus() 在名为 click 的事件的事件监听器中工作。

    2. 如果 intoFocus 为 true,则用户代理应该将 notification 相关的浏览上下文的视口带入焦点。

在整个 Web 平台中,"activate" 故意被错误命名为 "click"。

2.8. 关闭通知

通知被关闭时,无论是由底层通知平台还是由 终端用户关闭,都必须运行其关闭步骤

关闭步骤,用于给定的 通知 notification

  1. 如果通知列表包含 notification,则中止这些步骤。

  2. notification 处理 关闭事件

  3. 通知 列表移除 notification

给定通知 notification处理关闭 事件

  1. 如果 notification持久通知notification 被终端用户关闭,则给定 notification触发 Service Worker 通知 事件,事件名为 "notificationclose"。

  2. 如果 notification非持久通知,则队列任务以在代表 notificationNotification 对象上触发事件,事件名为 close

2.9. 提醒终端用户

提醒步骤,用于提醒终端用户关于给定的 通知 notification

  1. 使用 notification振动模式 (如果有),执行振动

3. API

[Exposed=(Window,Worker)]
interface Notification : EventTarget {
  constructor(DOMString title, optional NotificationOptions options = {});

  static readonly attribute NotificationPermission permission;
  [Exposed=Window] static Promise<NotificationPermission> requestPermission(optional NotificationPermissionCallback deprecatedCallback);

  static readonly attribute unsigned long maxActions;

  attribute EventHandler onclick;
  attribute EventHandler onshow;
  attribute EventHandler onerror;
  attribute EventHandler onclose;

  readonly attribute DOMString title;
  readonly attribute NotificationDirection dir;
  readonly attribute DOMString lang;
  readonly attribute DOMString body;
  readonly attribute USVString navigate;
  readonly attribute DOMString tag;
  readonly attribute USVString image;
  readonly attribute USVString icon;
  readonly attribute USVString badge;
  [SameObject] readonly attribute FrozenArray<unsigned long> vibrate;
  readonly attribute EpochTimeStamp timestamp;
  readonly attribute boolean renotify;
  readonly attribute boolean? silent;
  readonly attribute boolean requireInteraction;
  [SameObject] readonly attribute any data;
  [SameObject] readonly attribute FrozenArray<NotificationAction> actions;

  undefined close();
};

dictionary NotificationOptions {
  NotificationDirection dir = "auto";
  DOMString lang = "";
  DOMString body = "";
  USVString navigate;
  DOMString tag = "";
  USVString image;
  USVString icon;
  USVString badge;
  VibratePattern vibrate;
  EpochTimeStamp timestamp;
  boolean renotify = false;
  boolean? silent = null;
  boolean requireInteraction = false;
  any data = null;
  sequence<NotificationAction> actions = [];
};

enum NotificationPermission {
  "default",
  "denied",
  "granted"
};

enum NotificationDirection {
  "auto",
  "ltr",
  "rtl"
};

dictionary NotificationAction {
  required DOMString action;
  required DOMString title;
  USVString navigate;
  USVString icon;
};

callback NotificationPermissionCallback = undefined (NotificationPermission permission);

一个非持久通知由一个Notification 对象表示,可以通过Notification构造函数创建。

一个持久通知 由零个或多个Notification 对象表示,可以通过 showNotification() 方法创建。

3.1. 垃圾回收

通知列表 包含其对应的通知且其具有 事件监听器时,Notification 对象不得被垃圾回收。事件监听器的类型clickshowcloseerror

3.2. 构造函数

new Notification(title, options) 构造函数的步骤如下:

  1. 如果this相关全局对象ServiceWorkerGlobalScope 对象,则抛出一个TypeError

  2. 如果options["actions"] 不为空,则抛出一个TypeError

    操作仅支持持久通知

  3. notification使用设置对象创建通知的结果,给定titleoptionsthis相关设置对象

  4. thisnotification关联。

  5. 并行方式运行以下步骤:

    1. 如果获取通知权限状态的结果不是 "granted",则排队一个任务触发一个事件,事件名称为error,目标为this,然后中止这些步骤。

    2. notification运行通知显示步骤

3.3. 静态成员

静态permission获取器的步骤是返回 获取通知权限状态的结果。

如果您编辑标准,请避免复制上述内容。同步权限就像同步 IO,是一个糟糕的主意。

鼓励开发者使用权限query() 方法。[Permissions]

const permission = await navigator.permissions.query({name: "notifications"});
    if (permission.state === "granted") {
       // 我们有权限使用该 API…
    }
    

静态 requestPermission(deprecatedCallback) 方法的步骤如下:

  1. global当前全局对象

  2. promise一个新 promise,位于this相关 Realm中。

  3. 并行方式运行以下步骤:

    1. permissionState请求使用权限的结果,权限名称为"notifications"。

    2. 在全局任务队列中排队一个任务,任务源为DOM 操作任务源,目标为global, 以运行以下步骤:

      1. 如果deprecatedCallback已提供,则调用 deprecatedCallback,参数为« permissionState »和 "report"。

      2. 解析promise,值为 permissionState

  4. 返回promise

通知是目前唯一一个向终端用户提前请求权限是合理的实例。其他 API 的规范不应使用此模式,而应采用 许多更合适的替代方案

静态maxActions获取器的步骤是返回 支持的最大操作数

3.4. 对象成员

以下是 事件处理程序(及其对应的 事件处理程序事件类型),它们必须作为属性由 Notification 对象支持。

事件处理程序 事件处理程序事件类型
onclick click
onshow show
onerror error
onclose close

close() 方法的步骤是为this通知运行关闭步骤

title 获取器的步骤是返回this通知标题

dir 获取器的步骤是返回this通知方向

lang 获取器的步骤是返回this通知语言

body 获取器的步骤是返回this通知正文

navigate 获取器的步骤是:

  1. 如果this通知导航 URL为 null,则返回空字符串。

  2. 返回this通知导航 URL序列化后的结果。

tag 获取器的步骤是返回this通知标签

image 获取器的步骤是:

  1. 如果this通知没有图像 URL,则返回空字符串。

  2. 返回this通知图像 URL序列化后的结果。

icon 获取器的步骤是:

  1. 如果this通知没有图标 URL,则返回空字符串。

  2. 返回this通知图标 URL序列化后的结果。

badge 获取器的步骤是:

  1. 如果this通知没有徽章 URL,则返回空字符串。

  2. 返回this通知徽章 URL序列化后的结果。

vibrate 获取器的步骤是返回this通知振动模式

timestamp 获取器的步骤是返回this通知时间戳

renotify 获取器的步骤是返回this通知重新通知偏好

silent 获取器的步骤是返回this通知静音偏好

requireInteraction 获取器的步骤是返回this通知需要交互偏好

data 获取器的步骤是返回结构化反序列化(this通知数据, this相关 Realm)的结果。如果此操作抛出异常,则返回 null。

actions 获取器的步骤是:

  1. frozenActions 为一个类型为NotificationAction的空列表。

  2. 对于this通知操作中的每个entry

    1. action 为一个新的NotificationAction

    2. action["action"] 设置为entry名称

    3. action["title"] 设置为entry标题

    4. 如果entry导航 URL非 null,则将 action["navigate"] 设置为entry导航 URL序列化后的结果。

    5. 如果entry图标 URL非 null,则将action["icon"] 设置为entry图标 URL序列化后的结果。

    6. action上调用Object.freeze,以防止脚本意外修改。

    7. action附加到frozenActions

  3. 返回从frozenActions创建冻结数组的结果。

3.5. 示例

3.5.1. 在页面中使用事件

非持久性 Notification 对象在其生命周期内会分派事件,开发者可以利用这些事件来产生期望的行为。

当终端用户激活通知时,会分派 click 事件。

var not = new Notification("Gebrünn Gebrünn by Paul Kalkbrenner", { icon: "newsong.svg", tag: "song" });
    not.onclick = function() { displaySong(this); };

3.5.2. 在 service worker 中使用操作

持久通知会在 ServiceWorkerGlobalScope 上触发 notificationclick 事件。

这里,一个 service worker 显示一个带单个“存档”通知操作的通知,允许终端用户从通知中执行这个常见任务,而无需打开网站(例如,通知平台可能会在通知上显示一个按钮)。终端用户也可以激活通知的主体来打开他们的收件箱。

self.registration.showNotification("来自 Alice 的新邮件", {
      actions: [{action: 'archive', title: "存档"}]
    });

    self.addEventListener('notificationclick', function(event) {
      event.notification.close();
      if (event.action === 'archive') {
        silentlyArchiveEmail();
      } else {
        clients.openWindow("/inbox");
      }
    }, false);

3.5.3. 为多个实例使用 tag 成员

Web 应用程序经常在多个实例中并发运行,例如当终端用户在多个浏览器标签页中打开一个邮件应用程序时。由于桌面是共享资源,通知 API 提供了一种让这些实例轻松协调的方式,即使用 tag 成员。

代表相同概念事件的通知可以用相同的标签标记,当两者都显示时,终端用户只会收到一个通知。

实例 1                                   | 实例 2
                             |
    // 实例注意到有新邮件。                      |
    new Notification("来自 John Doe 的新邮件",   |
             { tag: 'message1' });       |
                             |
                             |  // 稍后,这个实例注意到
                             |  // 有新邮件。
                             |  new Notification("来自 John Doe 的新邮件",
                             |                   { tag: 'message1' });

如果用户代理遵循此处的算法,这种情况的结果是一个通知“来自 John Doe 的新邮件”。

3.5.4. 为单个实例使用 tag 成员

tag 成员也可以被应用程序的单个实例用来随着状态变化尽可能保持其通知的最新状态。

例如,如果 Alice 正在与 Bob 使用聊天应用程序,而 Bob 在 Alice 空闲时发送了多条消息,应用程序可能希望 Alice 不会为每条消息都看到桌面通知。

// Bob 说“嗨”
    new Notification("Bob: 嗨", { tag: 'chat_Bob' });

    // Bob 说“你今天下午有空吗?”
    new Notification("Bob: 嗨 / 你今天下午有空吗?", { tag: 'chat_Bob' });

这种情况的结果是一个通知;第二个通知替换了第一个具有相同标签的通知。在一个对通知进行排队(先进先出)的平台中,使用标签还允许通知保持其在队列中的位置。在最新通知最先显示的平台中,可以使用 close() 方法达到类似的结果。

4. Service Worker API

dictionary GetNotificationOptions {
  DOMString tag = "";
};

partial interface ServiceWorkerRegistration {
  Promise<undefined> showNotification(DOMString title, optional NotificationOptions options = {});
  Promise<sequence<Notification>> getNotifications(optional GetNotificationOptions filter = {});
};

[Exposed=ServiceWorker]
interface NotificationEvent : ExtendableEvent {
  constructor(DOMString type, NotificationEventInit eventInitDict);

  readonly attribute Notification notification;
  readonly attribute DOMString action;
};

dictionary NotificationEventInit : ExtendableEventInit {
  required Notification notification;
  DOMString action = "";
};

partial interface ServiceWorkerGlobalScope {
  attribute EventHandler onnotificationclick;
  attribute EventHandler onnotificationclose;
};

service worker 注册有一个关联的 是否已成功调用 showNotification() (一个布尔值),初始值为 false。

这是 Push API 标准的基础设施。[PUSH-API]

showNotification(title, options) 方法的步骤是:

  1. globalthis相关全局对象

  2. promise 为在this相关 Realm一个新的 promise

  3. 如果this活动 worker为 null,则用一个TypeError拒绝 promise 并返回 promise

  4. notification使用设置对象创建通知的结果,给定 titleoptionsthis相关设置对象。如果此操作抛出异常,则用该异常拒绝 promise 并返回 promise

  5. notificationservice worker 注册设置为 this

  6. 并行运行以下步骤:

    1. 如果获取通知权限状态的结果不是 "granted",则在DOM 操作任务源排队一个全局任务,给定 global 以用一个TypeError拒绝 promise,并中止这些步骤。

    2. notification 运行通知显示步骤

    3. notificationservice worker 注册是否已成功调用 showNotification() 设置为 true。

    4. DOM 操作任务源排队一个全局任务,给定 global 以用 undefined 解决 promise

  7. 返回 promise

getNotifications(filter) 方法的步骤是:

  1. globalthis相关全局对象

  2. realmthis相关 Realm

  3. originthis相关设置对象

  4. promise 为在 realm一个新的 promise

  5. 并行运行以下步骤:

    1. tagfilter["tag"]。

    2. notifications通知列表中所有通知列表,这些通知的origin 同源,其service worker 注册this,并且 如果 tag 不是空字符串,其标签tag

    3. DOM 操作任务源排队一个全局任务,给定 global 以运行以下步骤:

      1. objects 为一个列表

      2. 对于 notifications 中的每个通知,按创建顺序,用 realm 新建一个代表 通知Notification 对象,并将其追加objects

      3. objects 解决 promise

  6. 返回 promise

此方法返回零个或多个新的Notification 对象,这些对象可能代表与已存在的Notification 对象相同的底层通知


触发一个 service worker 通知事件,名为 name,给定一个 通知 notification,以及一个可选字符串 action(默认为空字符串):运行触发功能性事件,给定 nameNotificationEventnotificationservice worker 注册,以及以下初始化:

notification
一个代表 notification 的新 Notification 对象。
action
action

notification 获取器的步骤是返回其初始化时的值。

action 获取器的步骤是返回其初始化时的值。

以下是必须由 ServiceWorkerGlobalScope 对象作为属性支持的事件处理程序(及其对应的 事件处理程序事件类型):

事件处理程序 事件处理程序事件类型
onnotificationclick notificationclick
onnotificationclose notificationclose

致谢

感谢 Addison Phillips、 Aharon (Vladimir) Lanin、 Alex Russell、 Anssi Kostiainen、 Arkadiusz Michalski、 Boris Zbarsky、 Brady Eidson、 David Håsäther、 Doug Turner、 Drew Wilson、 Ehsan Akhgari、 Frederick Hirsch、 Ian Hickson、 Jake Archibald、 James Graham、 John Mellor、 Jon Lee、 Jonas Sicking、 Kagami Sascha Rosylight、 Michael Cooper、 Michael Henretty、 Michael™ Smith、 Michael van Ouwerkerk、 Mike Taylor、 Nicolás Satragno、 Olli Pettay、 Peter Beverloo、 Philip Jägenstedt、 Reuben Morais、 Rich Tibbett、 Robert Bindar、 박상현 (Sanghyun Park)、 Simon Pieters、 Theresa O’Connor、 timeless,以及 triple-underscore 的杰出贡献。

本标准由 Anne van Kesteren (Apple, annevk@annevk.nl) 撰写。 早期版本由 John Gregg (Google, johnnyg@google.com) 撰写。

知识产权

版权所有 © WHATWG (Apple, Google, Mozilla, Microsoft)。本作品根据 知识共享署名 4.0 国际许可协议授权。如果其部分内容被纳入源代码,则源代码中的这些部分将根据 BSD 3-Clause 许可证授权。

这是现行标准。对专利审查版本感兴趣的人应查看 现行标准审查草案

索引

本规范定义的术语

外部定义的术语

参考文献

规范性引用

[BCP47]
A. Phillips, Ed.; M. Davis, Ed.. 用于标识语言的标签。2009年9月。最佳当前实践。URL: https://www.rfc-editor.org/rfc/rfc5646
[BIDI]
Manish Goregaokar मनीष गोरेगांवकर; Robin Leroy. Unicode 双向算法。 2024年9月2日。Unicode 标准附录 #9。URL: https://www.unicode.org/reports/tr9/tr9-50.html
[DOM]
Anne van Kesteren. DOM 标准。现行标准。 URL: https://dom.spec.whatwg.org/
[FETCH]
Anne van Kesteren. Fetch 标准。现行标准。URL: https://fetch.spec.whatwg.org/
[HR-TIME]
Yoav Weiss. 高精度时间。URL: https://w3c.github.io/hr-time/
[HTML]
Anne van Kesteren; 等. HTML 标准。 现行标准。URL: https://html.spec.whatwg.org/multipage/
[INFRA]
Anne van Kesteren; Domenic Denicola. Infra 标准。现行标准。URL: https://infra.spec.whatwg.org/
[Permissions]
Marcos Caceres; Mike Taylor. 权限。URL: https://w3c.github.io/permissions/
[SERVICE-WORKERS]
Yoshisato Yanagisawa; Monica CHINTALA. Service Workers。URL: https://w3c.github.io/ServiceWorker/
[URL]
Anne van Kesteren. URL 标准。现行标准。 URL: https://url.spec.whatwg.org/
[VIBRATION]
Anssi Kostiainen. 振动 API。URL: https://w3c.github.io/vibration/
[WEBIDL]
Edgar Chen; Timothy Gu. Web IDL 标准。现行标准。 URL: https://webidl.spec.whatwg.org/

信息性参考文献

[PUSH-API]
Peter Beverloo;Martin Thomson;Marcos Caceres。Push API。网址:https://w3c.github.io/push-api/

IDL 索引

[Exposed=(Window,Worker)]
interface Notification : EventTarget {
  constructor(DOMString title, optional NotificationOptions options = {});

  static readonly attribute NotificationPermission permission;
  [Exposed=Window] static Promise<NotificationPermission> requestPermission(optional NotificationPermissionCallback deprecatedCallback);

  static readonly attribute unsigned long maxActions;

  attribute EventHandler onclick;
  attribute EventHandler onshow;
  attribute EventHandler onerror;
  attribute EventHandler onclose;

  readonly attribute DOMString title;
  readonly attribute NotificationDirection dir;
  readonly attribute DOMString lang;
  readonly attribute DOMString body;
  readonly attribute USVString navigate;
  readonly attribute DOMString tag;
  readonly attribute USVString image;
  readonly attribute USVString icon;
  readonly attribute USVString badge;
  [SameObject] readonly attribute FrozenArray<unsigned long> vibrate;
  readonly attribute EpochTimeStamp timestamp;
  readonly attribute boolean renotify;
  readonly attribute boolean? silent;
  readonly attribute boolean requireInteraction;
  [SameObject] readonly attribute any data;
  [SameObject] readonly attribute FrozenArray<NotificationAction> actions;

  undefined close();
};

dictionary NotificationOptions {
  NotificationDirection dir = "auto";
  DOMString lang = "";
  DOMString body = "";
  USVString navigate;
  DOMString tag = "";
  USVString image;
  USVString icon;
  USVString badge;
  VibratePattern vibrate;
  EpochTimeStamp timestamp;
  boolean renotify = false;
  boolean? silent = null;
  boolean requireInteraction = false;
  any data = null;
  sequence<NotificationAction> actions = [];
};

enum NotificationPermission {
  "default",
  "denied",
  "granted"
};

enum NotificationDirection {
  "auto",
  "ltr",
  "rtl"
};

dictionary NotificationAction {
  required DOMString action;
  required DOMString title;
  USVString navigate;
  USVString icon;
};

callback NotificationPermissionCallback = undefined (NotificationPermission permission);

dictionary GetNotificationOptions {
  DOMString tag = "";
};

partial interface ServiceWorkerRegistration {
  Promise<undefined> showNotification(DOMString title, optional NotificationOptions options = {});
  Promise<sequence<Notification>> getNotifications(optional GetNotificationOptions filter = {});
};

[Exposed=ServiceWorker]
interface NotificationEvent : ExtendableEvent {
  constructor(DOMString type, NotificationEventInit eventInitDict);

  readonly attribute Notification notification;
  readonly attribute DOMString action;
};

dictionary NotificationEventInit : ExtendableEventInit {
  required Notification notification;
  DOMString action = "";
};

partial interface ServiceWorkerGlobalScope {
  attribute EventHandler onnotificationclick;
  attribute EventHandler onnotificationclose;
};

MDN

Notification/Notification

In all current engines.

Firefox22+Safari7+Chrome20+
Opera23+Edge79+
Edge (Legacy)14+IENone
Firefox for Android?iOS Safari16.4+Chrome for AndroidNoneAndroid WebView?Samsung Internet?Opera Mobile?
MDN

Notification/actions

In only one current engine.

FirefoxNoneSafariNoneChrome53+
Opera39+Edge79+
Edge (Legacy)18IENone
Firefox for Android?iOS Safari?Chrome for Android?Android WebViewNoneSamsung Internet?Opera Mobile?

NotificationEvent/action

Firefox44+SafariNoneChrome48+
OperaNoneEdge79+
Edge (Legacy)17+IENone
Firefox for Android?iOS Safari?Chrome for Android?Android WebViewNoneSamsung Internet?Opera MobileNone
MDN

Notification/badge

In only one current engine.

FirefoxNoneSafariNoneChrome53+
Opera39+Edge79+
Edge (Legacy)18IENone
Firefox for Android?iOS Safari?Chrome for Android?Android WebViewNoneSamsung Internet?Opera Mobile?
MDN

Notification/body

In all current engines.

Firefox26+Safari11+Chrome33+
Opera23+Edge79+
Edge (Legacy)14+IENone
Firefox for Android?iOS Safari16.4+Chrome for Android42+Android WebViewNoneSamsung Internet?Opera Mobile?
MDN

Notification/click_event

In all current engines.

Firefox22+Safari7+Chrome20+
Opera23+Edge79+
Edge (Legacy)14+IENone
Firefox for Android?iOS SafariNoneChrome for Android42+Android WebViewNoneSamsung Internet?Opera Mobile?
MDN

Notification/close

In all current engines.

Firefox22+Safari7+Chrome20+
Opera23+Edge79+
Edge (Legacy)14+IENone
Firefox for Android?iOS Safari16.4+Chrome for Android42+Android WebViewNoneSamsung Internet?Opera Mobile?
MDN

Notification/close_event

In all current engines.

Firefox22+Safari7+Chrome20+
Opera23+Edge79+
Edge (Legacy)14+IENone
Firefox for Android?iOS SafariNoneChrome for Android42+Android WebViewNoneSamsung Internet?Opera Mobile?
MDN

Notification/data

In all current engines.

Firefox34+Safari16+Chrome44+
Opera?Edge79+
Edge (Legacy)16+IENone
Firefox for Android?iOS Safari16.4+Chrome for Android?Android WebViewNoneSamsung Internet?Opera Mobile?
MDN

Notification/dir

In all current engines.

Firefox26+Safari7+Chrome20+
Opera23+Edge79+
Edge (Legacy)14+IENone
Firefox for Android?iOS Safari16.4+Chrome for Android42+Android WebViewNoneSamsung Internet?Opera Mobile?
MDN

Notification/error_event

In all current engines.

Firefox22+Safari7+Chrome20+
Opera23+Edge79+
Edge (Legacy)14+IENone
Firefox for Android?iOS SafariNoneChrome for Android42+Android WebViewNoneSamsung Internet?Opera Mobile?
MDN

Notification/icon

In all current engines.

Firefox26+Safari11+Chrome33+
Opera23+Edge79+
Edge (Legacy)14+IENone
Firefox for Android?iOS Safari16.4+Chrome for Android42+Android WebViewNoneSamsung Internet?Opera Mobile?
MDN

Notification/image

In only one current engine.

FirefoxNoneSafariNoneChrome56+
Opera?Edge79+
Edge (Legacy)18IENone
Firefox for Android?iOS Safari?Chrome for Android?Android WebViewNoneSamsung Internet?Opera Mobile?
MDN

Notification/lang

In all current engines.

Firefox26+Safari11+Chrome33+
Opera23+Edge79+
Edge (Legacy)14+IENone
Firefox for Android?iOS Safari16.4+Chrome for Android42+Android WebViewNoneSamsung Internet?Opera Mobile?
MDN

Notification/maxActions_static

In only one current engine.

FirefoxNoneSafariNoneChrome48+
Opera?Edge79+
Edge (Legacy)18IENone
Firefox for Android?iOS Safari?Chrome for Android?Android WebViewNoneSamsung Internet?Opera Mobile?
MDN

Notification/permission_static

In all current engines.

Firefox22+Safari7+Chrome32+
Opera23+Edge79+
Edge (Legacy)14+IENone
Firefox for Android?iOS Safari16.4+Chrome for Android42+Android WebViewNoneSamsung Internet?Opera Mobile?
MDN

Notification/renotify

In only one current engine.

FirefoxNoneSafariNoneChrome50+
Opera?Edge79+
Edge (Legacy)?IENone
Firefox for Android?iOS Safari?Chrome for Android?Android WebViewNoneSamsung Internet?Opera Mobile?
MDN

Notification/requestPermission_static

In all current engines.

Firefox22+Safari15+Chrome20+
Opera23+Edge79+
Edge (Legacy)14+IENone
Firefox for Android22+iOS Safari16.4+Chrome for Android42+Android WebViewNoneSamsung Internet?Opera Mobile?
MDN

Notification/requireInteraction

In only one current engine.

FirefoxNoneSafariNoneChrome47+
Opera?Edge79+
Edge (Legacy)17+IENone
Firefox for Android?iOS Safari?Chrome for Android?Android WebViewNoneSamsung Internet?Opera Mobile?
MDN

Notification/show_event

In all current engines.

Firefox22+Safari7+Chrome20+
Opera23+Edge79+
Edge (Legacy)14+IENone
Firefox for Android?iOS SafariNoneChrome for Android42+Android WebViewNoneSamsung Internet?Opera Mobile?
MDN

Notification/silent

FirefoxNoneSafari17+Chrome43+
Opera?Edge79+
Edge (Legacy)17+IENone
Firefox for Android?iOS Safari?Chrome for Android?Android WebViewNoneSamsung Internet?Opera Mobile?
MDN

Notification/tag

In all current engines.

Firefox26+Safari7+Chrome20+
Opera23+Edge79+
Edge (Legacy)14+IENone
Firefox for Android?iOS SafariNoneChrome for Android42+Android WebViewNoneSamsung Internet?Opera Mobile?
MDN

Notification/timestamp

In only one current engine.

FirefoxNoneSafariNoneChrome50+
Opera?Edge79+
Edge (Legacy)17+IENone
Firefox for Android?iOS Safari?Chrome for Android?Android WebViewNoneSamsung Internet?Opera Mobile?
MDN

Notification/title

In all current engines.

Firefox26+Safari11+Chrome33+
Opera23+Edge79+
Edge (Legacy)14+IENone
Firefox for Android?iOS Safari16.4+Chrome for Android42+Android WebViewNoneSamsung Internet?Opera Mobile?
MDN

Notification/vibrate

In only one current engine.

FirefoxNoneSafariNoneChrome53+
Opera?Edge79+
Edge (Legacy)?IENone
Firefox for Android?iOS Safari?Chrome for Android53+Android WebViewNoneSamsung Internet6.0+Opera Mobile41+
MDN

Notification

In all current engines.

Firefox22+Safari7+Chrome20+
Opera23+Edge79+
Edge (Legacy)14+IENone
Firefox for Android?iOS Safari16.4+Chrome for AndroidNoneAndroid WebViewNoneSamsung InternetNoneOpera MobileNone
MDN

NotificationEvent/NotificationEvent

In all current engines.

Firefox44+SafariNoneChrome42+
Opera37+Edge79+
Edge (Legacy)17+IENone
Firefox for Android?iOS Safari16.4+Chrome for Android?Android WebViewNoneSamsung Internet?Opera Mobile37+
MDN

NotificationEvent/notification

Firefox44+SafariNoneChrome42+
Opera37+Edge79+
Edge (Legacy)17+IENone
Firefox for Android?iOS Safari?Chrome for Android?Android WebViewNoneSamsung Internet?Opera Mobile37+
MDN

NotificationEvent

In all current engines.

Firefox44+SafariNoneChrome42+
Opera37+Edge79+
Edge (Legacy)17+IENone
Firefox for Android?iOS Safari16.4+Chrome for Android?Android WebViewNoneSamsung Internet?Opera Mobile37+
MDN

ServiceWorkerGlobalScope/notificationclick_event

Firefox44+SafariNoneChrome40+
Opera24+Edge79+
Edge (Legacy)17+IENone
Firefox for Android?iOS SafariNoneChrome for Android?Android WebView?Samsung Internet?Opera Mobile?
MDN

ServiceWorkerGlobalScope/notificationclick_event

Firefox44+SafariNoneChrome40+
Opera24+Edge79+
Edge (Legacy)17+IENone
Firefox for Android?iOS SafariNoneChrome for Android?Android WebView?Samsung Internet?Opera Mobile?
MDN

ServiceWorkerGlobalScope/notificationclose_event

Firefox44+SafariNoneChrome50+
Opera34+Edge79+
Edge (Legacy)17+IENone
Firefox for Android?iOS SafariNoneChrome for Android?Android WebView?Samsung Internet?Opera Mobile34+
MDN

ServiceWorkerRegistration/getNotifications

In all current engines.

Firefox44+SafariNoneChrome40+
Opera?Edge79+
Edge (Legacy)17+IENone
Firefox for Android?iOS Safari16.4+Chrome for Android?Android WebViewNoneSamsung Internet?Opera Mobile?
MDN

ServiceWorkerRegistration/showNotification

In all current engines.

Firefox44+SafariNoneChrome42+
Opera?Edge79+
Edge (Legacy)17+IENone
Firefox for Android?iOS Safari16.4+Chrome for Android?Android WebViewNoneSamsung Internet?Opera Mobile?