Secure context
This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The Notification()
constructor creates a new Notification
object instance, which represents a user notification.
Syntax
var myNotification = new Notification(title, options);
Parameters
title
- Defines a title for the notification, which is shown at the top of the notification window.
options
Optional- An options object containing any custom settings that you want to apply to the notification. The possible options are:
dir
: The direction in which to display the notification. It defaults toauto
, which just adopts the browser's language setting behavior, but you can override that behaviour by setting values ofltr
andrtl
(although most browsers seem to ignore these settings.)lang
: The notification's language, as specified using aDOMString
representing a BCP 47 language tag. See the Sitepoint ISO 2 letter language codes page for a simple reference.badge
: AUSVString
containing the URL of the image used to represent the notification when there isn't enough space to display the notification itself.body
: ADOMString
representing the body text of the notification, which is displayed below the title.tag
: ADOMString
representing an identifying tag for the notification.icon
: AUSVString
containing the URL of an icon to be displayed in the notification.image
: aUSVString
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
: ABoolean
specifying whether the user should be notified after a new notification replaces an old one. The default isfalse
, 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 isfalse
.actions
: An array ofNotificationAction
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
: ABoolean
specifying whether the notification is silent (no sounds or vibrations issued), regardless of the device settings. The default isfalse
, which means it won't be silent.
Example
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 options
object and triggers the notification by 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 The definition of 'Notification() constructor' in that specification. |
Living Standard | Initial definition. |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Notification() constructor | Chrome
Full support
22
| Edge Full support ≤18 | Firefox
Full support
22
| IE No support No | Opera Full support 25 | Safari Full support 6 | WebView Android No support No | Chrome Android Full support Yes | Firefox Android
Full support
22
| Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes |
Legend
- Full support
- Full support
- No support
- No support
- Requires a vendor prefix or different name for use.
- Requires a vendor prefix or different name for use.
Chrome notes
Starting in Chrome 49, notifications don't work in incognito mode.
Internet Explorer notes
Version 38.14352 and higher of MS Edge Notification API is suported. Wikipedia - MS Edge
IE 11 and lower isn't supported.