安全上下文
此特征只可用于
安全上下文
(HTTPS),在某些或所有
支持浏览器
.
Notification()
构造函数创建新
Notification
object instance, which represents a user notification.
var myNotification = new Notification(title, options);
title
Defines a title for the notification, which is shown at the top of the notification window.
选项
可选
dir
: The direction in which to display the notification. It defaults to
auto
, which just adopts the browser's language setting behavior, but you can override that behaviour by setting values of
ltr
and
rtl
(although most browsers seem to ignore these settings.)
lang
: The notification's language, as specified using a
DOMString
representing a
BCP 47 language tag
. See the Sitepoint
ISO 2 letter language codes
page for a simple reference.
badge
: A
USVString
containing the URL of the image used to represent the notification when there isn't enough space to display the notification itself.
body
: A
DOMString
representing the body text of the notification, which is 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
:
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 with the notification.
renotify
: A
布尔
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.
requireInteraction
: Indicates that a notification should remain active until the user clicks or dismisses it, rather than closing automatically. The default value is
false
.
actions
: An array of
NotificationAction
s representing the actions available to the user when the notification is presented. These are options the user can choose among in order to act on the action within the context of the notification itself. The action's name is sent to the service worker notification handler to let it know the action was selected by the user.
silent
: A
布尔
specifying whether the notification is silent (no sounds or vibrations issued), regardless of the device settings. The default is
false
, which means it won't be silent.
In our
Emogotchi demo
(
see source code
), we run a
spawnNotification()
function when we want to trigger a notification. The function is passed parameters to specify the body, icon, and title we want, and then it creates the necessary
选项
object and triggers the notification by using the
Notification()
构造函数。
function spawnNotification(theBody,theIcon,theTitle) {
var options = {
body: theBody,
icon: theIcon
}
var n = new Notification(theTitle,options);
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
Notifications API
The definition of 'Notification() constructor' in that specification. |
实时标准 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
Notification()
构造函数
|
Chrome
22
|
Edge ≤18 |
Firefox
22
|
IE No | Opera 25 | Safari 6 | WebView Android No | Chrome Android Yes |
Firefox Android
22
|
Opera Android Yes | Safari iOS No | Samsung Internet Android Yes |
完整支持
不支持
要求使用供应商前缀或不同名称。
Starting in Chrome 49, notifications don't work in incognito mode.
Version 38.14352 and higher of MS Edge Notification API is suported. Wikipedia - MS Edge
IE 11 and lower isn't supported.
Notification
Notification()