ServiceWorkerRegistration: showNotification() method
Baseline
2023
*
Newly available
Since March 2023, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
* Some parts of this feature may have varying levels of support.
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 showNotification()
method of the
ServiceWorkerRegistration
interface creates a persistent notification associated with this service worker registration. If the user clicks on this notification, the notificationclick
event will be fired in this service worker's global scope.
Syntax
showNotification(title)
showNotification(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:
actions
Optional Experimental-
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.
icon
Optional-
A string containing the URL of an icon to display with the action.
If the user selects one of these actions, then the
action
property of the event passed to thenotificationclick
event handler will contain the selected action. badge
Optional Experimental-
A 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 accommodate devices up to 4x resolution, about 96x96px, and the image will be automatically masked.
body
Optional-
A string representing the body text of the notification, which is displayed below the title. The default is the empty string.
data
Optional Experimental-
Arbitrary data that you want associated with the notification. This can be of any structured-clonable data type. The default is
null
. dir
Optional-
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 ofltr
andrtl
(although most browsers seem to ignore these settings.) icon
Optional-
A string containing the URL of an icon to be displayed in the notification.
image
Optional Experimental-
A string containing the URL of an image to be displayed in the notification.
lang
Optional-
The notification's language, as specified using a BCP 47 language tag. The default is the empty string.
renotify
Optional Experimental-
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
, thentag
also must be set. requireInteraction
Optional Experimental-
Indicates that a notification should remain active until the user clicks or dismisses it, rather than closing automatically. The default value is
false
. silent
Optional-
A boolean value specifying whether the notification is silent (no sounds or vibrations issued), regardless of the device settings. The default,
null
, means to respect device defaults. Iftrue
, thenvibrate
must not be present. tag
Optional-
A string representing an identifying tag for the notification. The default is the empty string.
timestamp
Optional-
A 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
Optional Experimental-
A vibration pattern for the device's vibration hardware to emit with the notification. If specified,
silent
must not betrue
.
Return value
A Promise
that resolves to undefined
.
Exceptions
TypeError
-
Thrown if:
- The current service worker's state is not
activating
oractivated
. - The user has explicitly denied the browser's permission request to use the API.
- The
silent
option istrue
and thevibrate
option is specified. - The
renotify
option istrue
but thetag
option is empty.
- The current service worker's state is not
DataCloneError
DOMException
-
Thrown if serializing the
data
option failed for some reason.
Examples
navigator.serviceWorker.register("sw.js");
async function showNotification() {
const permission = await Notification.requestPermission();
if (permission === "granted") {
const registration = await navigator.serviceWorker.ready;
registration.showNotification("Vibration Sample", {
body: "Buzz! Buzz!",
icon: "../images/touch/chrome-touch-icon-192x192.png",
vibrate: [200, 100, 200, 100, 200, 100, 200],
tag: "vibration-sample",
});
}
}
Specifications
Specification |
---|
Notifications API> # dom-serviceworkerregistration-shownotification> |
Browser compatibility
Loading…