Notification.Notification()
Notification() 构造函数创建一个新的Notification
对象实例。
Note:
此特性在 Web Worker 中可用。语法
let myNotification = new Notification(title, options);
参数
- title
- 定义一个通知的标题,当它被触发时,它将显示在通知窗口的顶部。
- options 可选
- options对象包含应用于通知的任何自定义设置选项。选项有:
dir
: 显示通知的方向。默认是auto,跟随浏览器语言设置行为,你也可以通过设置ltr和rtl的值来覆盖该行为(虽然大多数浏览器似乎忽略这些设置)lang
: 通知的语言,如使用代表一个BCP 47语言标签的DOMString
指定的。请参阅Sitepoint ISO 2字母语言代码页面,以获得简单的参考。- badge: 一个
USVString
包含用于表示通知的图像的URL, 当没有足够的空间来显示通知本身时。 body
: 一个DOMString
表示通知的正文,将显示在标题下方。tag
: 一个DOMString
代表通知的 一个识别标签。icon
: 一个USVString
包含要在通知中显示的图标的URL。image
: 一个USVSTring
包含要在通知中显示的图像的URL。data
: 您想要与通知相关联的任意数据。这可以是任何数据类型。vibrate
: 一个振动模式 vibration pattern 设备的振动硬件在通知触发时发出。renotify
: 一个Boolean
指定在新通知替换旧通知后是否应通知用户。默认值为false,这意味着它们不会被通知。requireInteraction
: 表示通知应保持有效,直到用户点击或关闭它,而不是自动关闭。默认值为false。
以下选项列在最新规范中,但在任何浏览器中都不支持. It is advisable to keep checking back regularly to see if the status of these has updated, and let us know if you find any out of date information.
silent
: 一个Boolean
指明通知是否应该是无声的,即,不需要发出声音或振动,无论设备设置如何。默认值为false,这意味着它不会保持静默。sound
:一个USVString
包含通知触发时要播放的音频文件的URL。noscreen
: 一个Boolean
指定通知触发是否应启用设备的屏幕。 默认值为false,这意味着它将启用屏幕。sticky
: 一个Boolean
指明通知是否应该是“粘”, 即不易被用户清理。默认值为false,这意味着它不会粘。
Example
In our Emogotchi demo (see source code), we run a simple spawnNotification()
function when we want to fire a notification — this is passed arguments to specify the body, icon and title we want, then it creates the necessary options
object and fires the notification using the Notification()
constructor.
function spawnNotification(theBody,theIcon,theTitle) {
var options = {
body: theBody,
icon: theIcon
}
var n = new Notification(theTitle,options);
}
Specifications
Specification | Status | Comment |
---|---|---|
Notifications API Notification() |
Living Standard | Initial definition. |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 5 webkit (see notes) 22 |
4.0 moz (see notes) 22 |
未实现 | 25 | 6 (see notes) |
Available in workers | ? | 41.0 (41.0) | ? | ? | ? |
icon option |
5 webkit (see notes) 22 |
4.0 moz (see notes) 22 |
未实现 | 25 | 未实现 |
vibrate | 45.0 | 未实现 | 未实现 | 未实现 | 未实现 |
requireInteraction |
47.0 | 32 | |||
renotify |
50.0 | ||||
badge |
53.0 | 39.0 | |||
image |
55.0 | ? |
Feature | Android | Android Webview | Firefox Mobile (Gecko) | Firefox OS | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|
Basic support | ? |
(Yes) |
4.0 moz (see notes) 22 |
1.0.1 moz (see notes) 1.2 |
未实现 | ? | 未实现 |
(Yes) |
Available in workers | ? | ? | 41.0 (41.0) | ? | ? | ? | ? | ? |
icon option | ? | (Yes) | 4.0 moz (see notes) 22 |
1.0.1 moz (see notes) 1.2 |
未实现 | ? | 未实现 | (Yes) |
vibrate | 未实现 | 45.0 | 未实现 | 未实现 | 未实现 | 32 | 未实现 | 45.0 |
requireInteraction |
未实现 | 未实现 | 未实现 | |||||
renotify |
未实现 | 未实现 | 50.0 | |||||
badge |
未实现 | 53.0 | 39.0 | 53.0 | ||||
image |
未实现 | 未实现 | ? | 55.0 |
Firefox OS notes
{{Page("/en-US/docs/Web/API/Notifications_API", "Firefox OS notes")}}
Chrome notes
Starting in Chrome 49, notifications do not work in incognito mode.
{{Page("/en-US/docs/Web/API/Notifications_API", "Chrome notes")}}
Safari notes
{{Page("/en-US/docs/Web/API/Notifications_API", "Safari notes")}}