通知 API

现行标准 — 最后更新

参与:
GitHub whatwg/notifications (新问题, 未解决问题)
在 Matrix 上聊天
提交记录:
GitHub whatwg/notifications/commits
此提交的快照
@notifyapi
测试:
web-platform-tests notifications/ (正在进行的工作)
翻译 (非规范性):
日本語
简体中文

摘要

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

1. 术语

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

本规范中使用的一些术语在 DOM、Fetch、高分辨率时间、HTML、IDL、服务工作者、URL 和振动 API 标准中定义。[DOM] [FETCH] [HR-TIME] [HTML] [WEBIDL] [SERVICE-WORKERS] [URL] [VIBRATION]

2. 通知

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

一个通知具有一个关联的服务工作线程注册(null 或一个服务工作线程注册)。它最初为 null。

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

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

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

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

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

一个通知具有一个关联的数据(一个记录)。

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

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

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

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

一个通知具有一个关联的静默偏好(null 或一个布尔值)。它最初为 null。当为 true 时,表示不应发出声音或振动。当为 null 时,表示应根据平台约定来决定是否产生声音或振动。

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

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

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

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

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

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

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

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

名称
一个字符串。
标题
一个字符串。
图标 URL
Null 或一个URL。它最初为 null。
图标资源
Null 或一个资源。它最初为 null。

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

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

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

一个非持久性通知是一个通知,其服务工作线程注册为 null。

(这是一个跟踪向量。) 一个持久性通知是一个通知,其服务工作线程注册非 null。


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

  1. originsettings

  2. baseURLsettingsAPI 基础 URL

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

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

创建通知,给定一个字符串 titleNotificationOptions 字典 options、一个 origin、一个URL baseURL 以及一个EpochTimeStamp 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. notification标签设置为 options["tag"]。

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

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

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

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

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

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

  17. notification静默偏好设置为 options["silent"]。

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

  19. notification操作设置为 « »。

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

    1. action 为一个新的通知操作

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

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

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

    5. action 追加到 notification操作中。

  21. 返回 notification

2.1. 生命周期和UI集成

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

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

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

用户代理应持久化持久性通知,直到它们从通知列表中移除。

一个持久性通知可以调用其某个Notification对象的close()方法。

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

2.2. 权限集成

通知 API 是一个强大的功能,由名称“notifications”标识。[权限]

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

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

  2. 如果 permissionState 是“prompt”,则返回“default”。

  3. 返回 permissionState

2.3. 方向

本节内容的术语与HTML渲染部分中使用的术语等效。[HTML]

用户代理应遵循通知标题正文以及其每个操作标题文本的 Unicode 语义。在显示时,每个文本都应被视为一个或多个独立的双向算法段落,如双向算法规则 P1、P2 和 P3 所定义,例如,包括支持 U+000A 换行符 (LF) 字符的段落分隔行为。对于标题正文以及每个操作标题的每个段落,如果通知方向具有非“auto”的值,则它将提供对规则 P2 和 P3 的更高级别覆盖。[BIDI]

通知方向还决定了如果通知平台并排显示通知操作,则应向最终用户显示这些操作的相对顺序。

2.4. 语言

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

建议开发者只使用有效的语言标签。

2.5. 资源

给定通知 notification获取步骤如下:

  1. 如果通知平台支持图像,并且设置了图像 URL,则获取notification图像 URL

    目的是像获取 <img> 资源一样获取此资源,但这需要抽象化

    然后,并行地

    1. 等待响应

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

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

  2. 如果通知平台支持图标,并且设置了图标 URL,则获取notification图标 URL

    目的是像获取 <img> 资源一样获取此资源,但这需要抽象化

    然后,并行地

    1. 等待响应

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

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

  3. 如果通知平台支持徽章,并且设置了徽章 URL,则获取notification徽章 URL

    目的是像获取 <img> 资源一样获取此资源,但这需要抽象化

    然后,并行地

    1. 等待响应

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

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

  4. 如果通知平台支持操作和操作图标,则对于 notification操作中的每个 action:如果图标 URL非 null,则获取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. 如果 notification 是一个持久性通知,则:

    1. action 为空字符串。

    2. 如果用户激活了 notification操作之一,则将 action 设置为该通知操作名称

    3. 给定 notificationaction触发一个名为“notificationclick”的服务工作线程通知事件
  2. 否则,将一个任务排队以运行以下步骤:

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

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

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

在整个 Web 平台中,“激活”被有意地错误命名为“点击”。

2.8. 关闭通知

通知被底层通知平台或最终用户关闭时,必须为其运行关闭步骤

给定通知 notification关闭步骤如下:

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

  2. 使用 notification 处理关闭事件

  3. 通知列表移除 notification

处理关闭事件,给定一个通知 notification

  1. 如果 notification 是一个持久性通知并且 notification 是由最终用户关闭的,则给定 notification 触发一个名为“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 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 = "";
  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 icon;
};

callback NotificationPermissionCallback = undefined (NotificationPermission permission);

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

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

3.1. 垃圾回收

一个Notification对象在其对应的通知通知列表包含,并且它具有一个**类型**为 clickshowcloseerror事件侦听器时,不得进行垃圾回收。

3.2. 构造函数

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

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

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

    操作仅支持持久性通知

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

  4. notification 关联。

  5. 并行运行以下步骤:

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

    2. notification 运行通知显示步骤

3.3. 静态成员

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

如果您编辑标准,请不要复制以上内容。同步权限类似于同步 IO,是一个坏主意。

鼓励开发人员改用 Permissions query() 方法。[权限]

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

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

  1. global当前全局对象

  2. promise相关 Realm中的一个新 promise

  3. 并行运行以下步骤:

    1. permissionState请求使用notifications”权限的结果。

    2. 在给定 globalDOM 操作任务源将一个全局任务排队以运行以下步骤:

      1. 如果给定了 deprecatedCallback,则使用 « permissionState » 和 “report调用 deprecatedCallback

      2. 使用 permissionState 解决 promise

  4. 返回 promise

通知是目前为止唯一一个在请求用户权限时有意义的实例。其他API的规范不应使用这种模式,而应采用更多合适的替代方案之一。

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

3.4. 对象成员

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

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

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

title getter 步骤是返回通知标题

dir getter 步骤是返回通知方向

lang getter 步骤是返回通知语言

body getter 步骤是返回通知正文

tag getter 步骤是返回通知标签

image getter 步骤如下:

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

  2. 返回通知图像 URL,并进行序列化

icon getter 步骤如下:

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

  2. 返回通知图标 URL,并进行序列化

badge getter 步骤如下:

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

  2. 返回通知徽章 URL,并进行序列化

vibrate getter 步骤是返回通知振动模式

timestamp getter 步骤是返回通知时间戳

renotify getter 步骤是返回通知重新通知偏好

silent getter 步骤是返回通知静默偏好

requireInteraction getter 步骤是返回通知需要交互偏好

data getter 步骤是返回StructuredDeserialize(通知数据, 相关 Realm)。如果此操作引发异常,则返回 null。

actions getter 步骤如下:

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

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

    1. action 为一个新的 NotificationAction

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

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

    4. 如果 entry图标 URL 非 null,则将 action["icon"] 设置为 entry图标 URL,并进行序列化

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

    6. action 追加到 frozenActions

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

3.5. Examples

3.5.1. Using events from a page

非持久化 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. Using actions from a service worker

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

此处,服务工作线程显示一个带有单个“存档”通知操作的通知,允许最终用户直接从通知执行此常见任务,而无需打开网站(例如,通知平台可能会在通知上显示一个按钮)。最终用户还可以激活通知的主体以打开其收件箱。

self.registration.showNotification("New mail from Alice", {
  actions: [{action: 'archive', title: "Archive"}]
});

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("New mail from John Doe",   |
                 { tag: 'message1' });       |
                                             |
                                             |  // 稍后,此实例注意到有新邮件。
                                             |  new Notification("New mail from John Doe",
                                             |                   { tag: 'message1' });

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

3.5.4. 使用 tag 成员来处理单个实例

tag 成员还可以由单个实例的应用程序使用,以便在状态更改时保持其通知尽可能最新。

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

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

// Bob 说 "你今天下午有空吗?"
new Notification("Bob: Hi / 你今天下午有空吗?", { 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;
};

showNotification(title, options) 方法步骤如下:

  1. global相关全局对象

  2. promise相关 Realm 中的一个新 promise

  3. 如果活动工作线程为 null,则用一个 TypeError 拒绝 promise 并返回 promise

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

  5. notification服务工作线程注册设置为

  6. 并行运行以下步骤:

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

    2. notification 运行通知显示步骤

    3. 在给定 globalDOM 操作任务源将一个全局任务排队以用 undefined 解决 promise

  7. 返回 promise

getNotifications(filter) 方法步骤如下:

  1. global相关全局对象

  2. realm相关 Realm

  3. origin相关设置对象

  4. promiserealm 中的一个新 promise

  5. 并行运行以下步骤:

    1. tagfilter["tag"]。

    2. notifications通知列表中所有通知列表,这些通知的origin 同源,其服务工作线程注册,并且如果 tag 不是空字符串,则其标签tag

    3. 在给定 globalDOM 操作任务源将一个全局任务排队以运行以下步骤:

      1. objects 为一个列表

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

      3. objects 解决 promise

  6. 返回 promise

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


触发服务工作线程通知事件(名为 name),给定一个通知 notification,以及一个可选字符串 action(默认为空字符串):运行触发功能事件,给定 nameNotificationEventnotification服务工作线程注册以及以下初始化:

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

notification getter 步骤是返回其初始化时的值。

action getter 步骤是返回其初始化时的值。

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

事件处理器 事件类型
onnotificationclick notificationclick
onnotificationclose notificationclose

致谢

感谢 Addison Phillips, Aharon (Vladimir) Lanin, Alex Russell, Anssi Kostiainen, Arkadiusz Michalski, Boris Zbarsky, David Håsäther, Doug Turner, Drew Wilson, Ehsan Akhgari, Frederick Hirsch, Ian Hickson, Jake Archibald, James Graham, John Mellor, Jon Lee, Jonas Sicking, 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, and triple-underscore for being awesome.

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

知识产权

版权所有 © WHATWG (Apple, Google, Mozilla, Microsoft)。本作品采用 知识共享署名 4.0 国际许可协议 授权。若本作品的部分内容被纳入源代码中,则该部分内容在源代码中依据 BSD 3-Clause License 授权。

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

Index

Terms defined by this specification

Terms defined by reference

References

Normative References

[BCP47]
A. Phillips, Ed.; M. Davis, Ed.. Tags for Identifying Languages. September 2009. Best Current Practice. URL: https://www.rfc-editor.org/rfc/rfc5646
[BIDI]
Manish Goregaokar मनीष गोरेगांवकर; Robin Leroy. Unicode Bidirectional Algorithm. 2 September 2024. Unicode Standard Annex #9. URL: https://www.unicode.org/reports/tr9/tr9-50.html
[DOM]
Anne van Kesteren. DOM Standard. Living Standard. URL: https://dom.spec.whatwg.org/
[FETCH]
Anne van Kesteren. Fetch Standard. Living Standard. URL: https://fetch.spec.whatwg.org/
[HR-TIME]
Yoav Weiss. High Resolution Time. URL: https://w3c.github.io/hr-time/
[HTML]
Anne van Kesteren; et al. HTML Standard. Living Standard. URL: https://html.spec.whatwg.org/multipage/
[INFRA]
Anne van Kesteren; Domenic Denicola. Infra Standard. Living Standard. URL: https://infra.spec.whatwg.org/
[Permissions]
Marcos Caceres; Mike Taylor. Permissions. URL: https://w3c.github.io/permissions/
[SERVICE-WORKERS]
Jake Archibald; Marijn Kruisselbrink. Service Workers. URL: https://w3c.github.io/ServiceWorker/
[URL]
Anne van Kesteren. URL Standard. Living Standard. URL: https://url.spec.whatwg.org/
[VIBRATION]
Anssi Kostiainen. Vibration API (Second Edition). URL: https://w3c.github.io/vibration/
[WEBIDL]
Edgar Chen; Timothy Gu. Web IDL Standard. Living Standard. URL: https://webidl.spec.whatwg.org/

IDL Index

[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 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 = "";
  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 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?