Notification.Notification()

这篇翻译不完整。请帮忙从英语翻译这篇文章

Notification() 构造函数创建一个新的Notification对象实例。

Note: This feature is available in Web Workers.

语法

var myNotification = new Notification(title, options);

参数

title
定义通知的标题,会在通知窗口顶部显示。
options 可选
options对象包含应用于通知的任何自定义设置选项。选项有:
  • dir: 显示通知的方向。默认是auto,跟随浏览器语言设置习惯,你也可以设置为ltr和rtl(部分浏览器不适用)
  • lang: 通知的语言,
  • badge: A USVString containing the URL of the image used to represent the notification when there is not enough space to display thenotification itself.
  • body: A DOMString representing the body text of the notification, which will be displayed below the title.
  • tag: A DOMString representing an identifying tag for the notification.
  • icon: A USVString containing the URL of an icon to be displayed in the notification.
  • image: a USVSTring containing the URL of an image to be displayed in the notification.
  • data: Arbitrary data that you want associated with the notification. This can be of any data type.
  • vibrate: A vibration pattern for the device's vibration hardware to emit when the notification fires.
  • renotify: A Boolean specifying whether the user should be notified after a new notification replaces an old one. The default is false, which means they won't be notified.

The following options are listed in the most up-to-date spec, but are not supported in any browsers yet. 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: A Boolean specifying whether the notification should be silent, i.e. no sounds or vibrations should be issued, regardless of the device settings. The default is false, which means it won't be silent.
  • sound: A USVString containing the URL of an audio file to be played when the notification fires.
  • noscreen: A Boolean specifying whether the notification firing should enable the device's screen or not. The default is false, which means it will enable the screen.
  • sticky: A Boolean specifying whether the notification should be 'sticky', i.e. not easily clearable by the user. The default is false, which means it won't be sticky.

The following option is non-standard but in a draft version of the spec.

  • requireInteraction: Indicates that on devices with sufficiently large screens, a notification should remain active until the user clicks or dismisses it. If this value is absent or false, the desktop version of Chrome will auto-minimize notifications after approximately eight seconds. The default value is 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

Chrome notes

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

Safari notes

See also

文档标签和贡献者

 此页面的贡献者: Jiang-Xuan, fengwuxin
 最后编辑者: Jiang-Xuan,