Notifications API - Web APIs 编辑

Note:

This feature is available in Web Workers.

Secure context

This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

The Notifications API allows web pages to control the display of system notifications to the end user. These are outside the top-level browsing context viewport, so therefore can be displayed even when the user has switched tabs or moved to a different app. The API is designed to be compatible with existing notification systems, across different platforms.

Concepts and usage

On supported platforms, showing a system notification generally involves two things. First, the user needs to grant the current origin permission to display system notifications, which is generally done when the app or site initialises, using the Notification.requestPermission() method. This should be done in response to a user gesture, such as clicking a button, for example:

btn.addEventListener('click', function() {
  let promise = Notification.requestPermission();
  // wait for permission
})

This is not only best practice — you should not be spamming users with notifications they didn't agree to — but going forward browsers will explicitly disallow notifications not triggered in response to a user gesture. Firefox is already doing this from version 72, for example.

This will spawn a request dialog, along the following lines:

From here the user can choose to allow notifications from this origin, or block them. Once a choice has been made, the setting will generally persist for the current session.

Note: As of Firefox 44, the permissions for Notifications and Push have been merged. If permission is granted for notifications, push will also be enabled.

Next, a new notification is created using the Notification() constructor. This must be passed a title argument, and can optionally be passed an options object to specify options, such as text direction, body text, icon to display, notification sound to play, and more.

In addition, the Notifications API spec specifies a number of additions to the ServiceWorker API, to allow service workers to fire notifications.

Note: To find out more about using notifications in your own app, read Using the Notifications API.

Notifications interfaces

Notification
Defines a notification object.

Service worker additions

ServiceWorkerRegistration
Includes the ServiceWorkerRegistration.showNotification() and ServiceWorkerRegistration.getNotifications() method, for controlling the display of notifications.
ServiceWorkerGlobalScope
Includes the ServiceWorkerGlobalScope.onnotificationclick handler, for firing custom functions when a notification is clicked.
NotificationEvent
A specific type of event object, based on ExtendableEvent, which represents a notification that has fired.

Specifications

SpecificationStatusComment
Notifications APILiving StandardLiving standard

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:53 次

字数:5830

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文