|
JS 显示气泡通知的Notification API
Notifications API 允许网页控制向最终用户显示系统通知 —这些都在顶级浏览上下文视口之外,因此即使用户已经切换标签页或移动到不同的应用程序,也可以显示。该 API 被设计成与不同平台上的现有通知系统兼容。
不过,这须得在安全上下文(HTTPS)下,浏览器才允许. 不妨去弄一个SSL证书(有免费的,如Let's Encrypt),很简单的
在支持该接口的平台上,显示一个系统通知通常涉及两件事。首先,用户需要授予当前源的权限以显示系统通知,这通常在应用或站点初始化时,使用Notification.requestPermission() 方法来完成。
这将产生一个权限请求对话框:
从这里,用户可以选择允许来自此来源的通知,阻止来自此来源的通知,或不选择此点。一旦做出选择,该设置通常将持续用于当前会话。
The Notifications API lets a web page or app send notifications that are displayed outside the page at the system level; this lets web apps send information to a user even if the application is idle or in the background(即使浏览器在后台运行的情况下). This article looks at the basics of using this API in your own apps.
Typically, system notifications refer to the operating system's standard notification mechanism: think for example of how a typical desktop system or mobile device broadcasts notifications.
The system notification system will vary of course by platform and browser, but this is OK, and the Notifications API is written to be general enough for compatibility with most system notification systems.
Examples
One of the most obvious use cases for web notifications is a web-based mail or IRC application that needs to notify the user when a new message is received, even if the user is doing something else with another application. Many examples of this now exist, such as Slack.
developer.mozilla.org/en-US/docs/Web/API/Notifications_API/Using_the_Notifications_API |
|