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.
Warning: This constructor throws a TypeError when called in nearly all mobile browsers. Instead, you need to register a service worker and use ServiceWorkerRegistration.showNotification().
new Notification(title)
new Notification(title, options)titleDefines a title for the notification, which is shown at the top of the notification window.
options OptionalAn options object containing any custom settings that you want to apply to the notification. The possible options are:
actions OptionalMust be unspecified or an empty array. actions is only supported for persistent notifications fired from a service worker using ServiceWorkerRegistration.showNotification().
badge OptionalA string containing the URL of the image used to represent the notification when there isn't enough space to display the notification itself; for example, the Android Notification Bar. On Android devices, the badge should support devices with up to 4x resolution, about 96x96px, and the image will be automatically masked.
body OptionalA string representing the body text of the notification, which is displayed below the title. The default is the empty string.
data OptionalArbitrary data that you want associated with the notification. This can be of any structured-clonable data type. The default is null.
dir OptionalThe 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 of ltr and rtl (although most browsers seem to ignore these settings.)
icon OptionalA string containing the URL of an icon to be displayed in the notification.
image OptionalA string containing the URL of an image to be displayed in the notification.
lang OptionalThe notification's language, as specified using a string representing a BCP 47 language tag. The default is the empty string.
A string containing a URL to navigate to when the user activates the notification. When set, the user agent navigates to this URL instead of firing the click event. The value is parsed relative to the base URL of the page. See Notification.navigate for more information.
renotify OptionalA 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. If true, tag must also be set.
requireInteraction OptionalIndicates that a notification should remain active until the user clicks or dismisses it, rather than closing automatically. The default value is false.
silent OptionalA boolean value specifying whether the notification should be silent, i.e., no sounds or vibrations should be issued regardless of the device settings. If set to true, the notification is silent; if set to null (the default value), the device's default settings are respected.
tag OptionalA string representing an identifying tag for the notification. The default is the empty string.
timestamp OptionalA timestamp, given as Unix time in milliseconds, representing the time associated with the notification. This could be in the past when a notification is used for a message that couldn't immediately be delivered because the device was offline, or in the future for a meeting that is about to start.
vibrate OptionalA vibration pattern for the device's vibration hardware to emit with the notification. If specified, silent must not be true.
An instance of the Notification object.
TypeErrorThrown if:
ServiceWorkerGlobalScope.actions option is specified and is not empty.silent option is true and the vibrate option is specified.renotify option is true but the tag option is empty.DataCloneError DOMExceptionThrown if serializing the data option failed for some reason.
The constructor creates a new Notification object instance, which represents a user notification.
You must get permission to display notifications using Notification.requestPermission(). The permission may not be grantable, for example if the page is in private browsing mode.
This constructor throws a TypeError when called in nearly all mobile browsers, and this is unlikely to change, because web pages on mobile devices almost never "run in the background", which is the main use case for notifications. Instead, you need to register a service worker and use ServiceWorkerRegistration.showNotification(). See Chrome issue #481856 for more information.
For more examples, see the Notification page and Using the Notifications API.
This is a basic example that shows a notification if permission is already granted. This will not work on mobile devices.
if (Notification.permission === "granted") {
const notification = new Notification("Hi there!");
}This example shows a more robust approach that allows showing notifications on both desktop and mobile devices.
First we check if Notification is supported, and if permission has been granted, returning early if either condition is not met. We then check if there is an active service worker. If so, we use it to call ServiceWorkerRegistration.showNotification(); if not, we fall back to calling the constructor.
async function showNotification(title, options = {}) {
if (!("Notification" in window)) return;
if (Notification.permission !== "granted") return;
// Only use SW if one is already active — don't hang waiting
const swReg = navigator.serviceWorker?.controller
? await navigator.serviceWorker.getRegistration()
: null;
if (swReg) {
await swReg.showNotification(title, options);
} else {
new Notification(title, options);
}
}Note that this will still throw an error on a mobile device if the page does not have a service worker ready. Depending on your application, you might wrap this code in a try...catch block.