Skip to main content

Notifications API - 允许网页控制向终端用户显示的系统通知

备注: 此特性在 Web Worker 中可用

安全上下文

此项功能仅在一些支持的浏览器安全上下文(HTTPS)中可用。

Notifications API(通知 API)允许网页控制向终端用户显示的系统通知。这些通知位于顶级浏览上下文视口之外,因此即使用户切换选项卡或移动到不同的应用程序也可以显示。API 旨在与现有通知系统兼容,以及跨平台。

概念和用法

在支持的平台上,显示系统通知通常涉及两件事。首先,用户需要授予当前源显示系统通知的权限,这通常在应用程序或站点初始化时完成,使用 Notification.requestPermission() 方法。它应该响应用户手势来完成,例如单击按钮,例如:

btn.addEventListener('click', function() {
let promise = Notification.requestPermission();
// 等待许可
})

这不仅是最佳实践 —— 你不应该用他们不同意的通知向用户发送垃圾信息 —— 而且浏览器将明确禁止未响应用户手势触发的通知。例如,Firefox 已经从 72 版开始这样做。

这将生成一个请求对话框,如下所示:

从这里,用户可以选择允许来自该来源的通知,或阻止它们。一旦做出选择,该设置通常会在当前会话中持续存在。

注意: 从 Firefox 44 开始,通知和 Push 的权限已合并。如果授予通知权限,也将启用推送。

接下来,使用 Notification() 构造函数创建一个新通知。它必须传递一个标题参数,并且可以选择传递一个选项对象来指定选项,例如文本方向、正文、要显示的图标、要播放的通知声音等。

此外,通知 API 规范为 ServiceWorker API 指定了许多附加内容,以允许服务工作线程触发通知。

注意: 要了解有关在您自己的应用程序中使用通知的更多信息,请阅读 使用通知 API

通知接口

Notification

定义一个通知对象。

服务工作线程扩展

ServiceWorkerRegistration

包括 ServiceWorkerRegistration.showNotification()ServiceWorkerRegistration.getNotifications() 方法,用于控制显示通知。

ServiceWorkerGlobalScope

包括 ServiceWorkerGlobalScope.onnotificationclick 处理程序,用于在单击通知时触发自定义函数。

NotificationEvent

一种特定类型的事件对象,基于 ExtendableEvent,表示已触发的通知。

规范

规范
Notifications API Living Standard

桌面浏览器兼容性

特性ChromeEdgeFirefoxInternet ExplorerOperaSafari
基础支持201 214

22

4moz

不支持234 57
Notification() 构造函数2014

22

4moz

不支持237
actions5318不支持不支持39不支持
badge5318不支持不支持39不支持
body331426不支持2311
click 事件201422不支持237
close201422不支持237
close 事件201422不支持237
data441634不支持3116
dir201426不支持237
error 事件201422不支持237
icon331426不支持2311
image5318不支持不支持40不支持
lang331426不支持2311
maxActions4818不支持不支持35不支持
permission321422不支持237
renotify5079不支持不支持37不支持
requestPermission20142211 12不支持23

15

7 — 1515

requireInteraction4717不支持不支持34不支持
需要安全上下文627967不支持49不支持
show 事件201422不支持237
silent4317不支持不支持30不支持
tag201426不支持237
timestamp5017不支持不支持37不支持
title331426不支持2311
vibrate5379不支持不支持40不支持
在 Worker 中可用421541不支持29不支持

移动浏览器兼容性

特性AndroidChrome for AndroidEdge mobileFirefox for AndroidIE mobileOpera AndroidiOS Safari
基础支持不支持10421 3未知

22

4moz

未知296 7不支持
Notification() 构造函数不支持不支持未知

22

4moz

未知不支持不支持
actions不支持53未知不支持未知41不支持
badge不支持53未知不支持未知41不支持
body不支持42未知26未知29不支持
click 事件不支持42未知22未知29不支持
close不支持42未知22未知29不支持
close 事件不支持42未知22未知29不支持
data不支持44未知34未知32不支持
dir不支持42未知26未知29不支持
error 事件不支持42未知22未知29不支持
icon不支持42未知26未知29不支持
image不支持53未知不支持未知41不支持
lang不支持42未知26未知29不支持
maxActions不支持48未知不支持未知35不支持
permission不支持42未知22未知29不支持
renotify不支持50未知不支持未知37不支持
requestPermission不支持42未知2213 14未知29不支持
requireInteraction不支持47未知不支持未知34不支持
需要安全上下文不支持62未知67未知46不支持
show 事件不支持42未知22未知29不支持
silent不支持43未知不支持未知30不支持
tag不支持42未知26未知29不支持
timestamp不支持50未知不支持未知37不支持
title不支持42未知26未知29不支持
vibrate不支持5316未知不支持未知4116不支持
在 Worker 中可用不支持42未知41未知29不支持

1. Starting in Chrome 49, notifications do not work in incognito mode.

2. Before Chrome 42, service worker additions were not supported.

3. Notifications in Chrome for Android are only available through service workers. To show notifications on Android, see ServiceWorkerRegistration.showNotification().

4. Starting in Opera 36, notifications do not work in incognito mode.

5. Before Opera 29, service worker additions were not supported.

6. Notifications in Opera for Android are only available through service workers. To show notifications on Android, see ServiceWorkerRegistration.showNotification().

7. Starting in Opera for Android 36, notifications do not work in incognito mode.

8. Notifications in Samsung Internet are only available through service workers. To show notifications on Android, see ServiceWorkerRegistration.showNotification().

9. Starting in Samsung Internet 5.0, notifications do not work in incognito mode.

10. See bug 551446.

11. From Firefox 70 onwards, cannot be called from a cross-origin iframe.

12. 从 Firefox 72 开始,只能在响应用户手势(例如 click 事件)时调用。

13. From Firefox Android 79 onwards, cannot be called from a cross-origin iframe.

14. From Firefox Android 79 onwards, can only be called in response to a user gesture such as a click event.

15. Only supported the deprecated callback syntax.

16. 无论 Chrome 版本是多少,都无法在 Android O 或更高版本上使用。

相关链接