Notification: Notification() constructor
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Note: This feature is available in Web Workers.
The Notification() constructor creates a new
Notification object instance, which represents a user notification.
Note: Trying to create a notification inside the ServiceWorkerGlobalScope using the Notification() constructor will throw a TypeError. Use ServiceWorkerRegistration.showNotification() instead.
Syntax
new Notification(title)
new Notification(title, options)
Parameters
title-
Defines a title for the notification, which is shown at the top of the notification window.
optionsOptional-
An options object containing any custom settings that you want to apply to the notification. The possible options are:
dirOptional-
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 behavior by setting values ofltrandrtl(although most browsers seem to ignore these settings.) langOptional-
The notification's language, as specified using a string representing a language tag according to RFC 5646: Tags for Identifying Languages (also known as BCP 47). See the Sitepoint ISO 2 letter language codes page for a simple reference. The default is the empty string.
badgeOptional-
A string containing the URL of the image used to represent the notification when there isn't enough space to display the notification itself.
bodyOptional-
A string representing the body text of the notification, which is displayed below the title. The default is the empty string.
tagOptional-
A string representing an identifying tag for the notification. The default is the empty string.
iconOptional-
A string containing the URL of an icon to be displayed in the notification.
imageOptional-
a string containing the URL of an image to be displayed in the notification.
dataOptional-
Arbitrary data that you want associated with the notification. This can be of any data type. The default is
null. vibrateOptional-
A vibration pattern for the device's vibration hardware to emit with the notification. If specified,
silentmust not betrue. timestampOptional-
A number representing the time at which a notification is created or applicable (past, present, or future).
renotifyOptional-
A boolean value 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. Iftrue, thentagalso must be set. requireInteractionOptional-
Indicates that a notification should remain active until the user clicks or dismisses it, rather than closing automatically. The default value is
false. actionsOptional-
An array of actions to display in the notification, for which the default is an empty array. Each element in the array can be an object with the following members:
action-
A string identifying a user action to be displayed on the notification.
title-
A string containing action text to be shown to the user.
iconOptional-
A string containing the URL of an icon to display with the action.
Appropriate responses are built using
event.actionwithin thenotificationclickevent. silentOptional-
A boolean value specifying whether the notification is silent (no sounds or vibrations issued), regardless of the device settings. The default is
null. Iftrue, thenvibratemust not be present.
Return value
An instance of the Notification object.
Exceptions
TypeError-
Thrown if:
- The constructor is called within the
ServiceWorkerGlobalScope. - The
actionsoption is specified and is not empty. - The
silentoption istrueand thevibrateoption is specified. - The
renotifyoption istruebut thetagoption is empty.
- The constructor is called within the
DataCloneErrorDOMException-
Thrown if serializing the
dataoption failed for some reason.
Examples
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(body, icon, title) {
const notification = new Notification(title, { body, icon });
}
Specifications
| Specification |
|---|
| Notifications API Standard # dom-notification-notification |
Browser compatibility
| desktop | mobile | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
Notification() constructor | |||||||||||
Chrome notes
Starting in Chrome 49, notifications don't work in incognito mode.
Chrome for Android will throw a TypeError when calling the
Notification constructor. It only supports creating
notifications from a service worker. See the
Chromium issue tracker for more details.