ServiceWorkerRegistration: showNotification() method

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2023.

* 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 notification on an active service worker.

Syntax

js
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

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.

A string containing a URL to navigate to when the user activates this action. When set, the user agent navigates to this URL instead of firing the notificationclick event See Notification.navigate for more information.

Appropriate responses are built using event.action within the notificationclick event.

badge Optional

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

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 of ltr and rtl (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

A string containing the URL of an image to be displayed in the notification.

lang Optional

The 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 notificationclick event. The value is parsed relative to the base URL of the service worker. See Notification.navigate for more information.

renotify Optional

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. If true, then tag also must be set.

requireInteraction Optional

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. If true, then vibrate 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

A vibration pattern for the device's vibration hardware to emit with the notification. If specified, silent must not be true.

Return value

A Promise that resolves to undefined.

Exceptions

TypeError

Thrown if:

  • The current service worker's state is not activating or activated.
  • The user has explicitly denied the browser's permission request to use the API.
  • The silent option is true and the vibrate option is specified.
  • The renotify option is true but the tag option is empty.
DataCloneError DOMException

Thrown if serializing the data option failed for some reason.

Examples

js
navigator.serviceWorker.register("sw.js");

function showNotification() {
  Notification.requestPermission().then((result) => {
    if (result === "granted") {
      navigator.serviceWorker.ready.then((registration) => {
        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",
        });
      });
    }
  });
}

To invoke the above function at an appropriate time, you could listen to the notificationclick event.

You can also retrieve details of the Notifications that have been fired from the current service worker using ServiceWorkerRegistration.getNotifications().

Specifications

Specification
Notifications API
# dom-serviceworkerregistration-shownotification

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
showNotification
Chrome – Full support
Chrome 42 (Release date: 2015-04-14)
footnote Full support
Edge – Full support
Edge 17 (Release date: 2018-04-30)
footnote Full support
Firefox – Full support
Firefox 44 (Release date: 2016-01-26)
footnote Full support
Opera – Full support
Opera 29 (Release date: 2015-04-28)
footnote Full support
Safari – Full support
Safari 16 (Release date: 2022-09-12)
footnote
footnote Notifications are supported on macOS Ventura and later.
Chrome Android – Full support
Chrome Android 42 (Release date: 2015-04-15)
footnote Full support
Firefox for Android – Full support
Firefox for Android 44 (Release date: 2016-01-26)
footnote Full support
Opera Android – Full support
Opera Android 29 (Release date: 2015-04-28)
footnote Full support
Safari on iOS – Full support
Safari on iOS 16.4 (Release date: 2023-03-27)
footnote
footnote Notifications are supported in web apps saved to the home screen.
Samsung Internet – Full support
Samsung Internet 4 (Release date: 2016-03-11)
footnote Full support
WebView Android – No support
WebView Android
footnote
footnote See bug 40443309
WebView on iOS – No support
WebView on iOS
footnote
footnote Notifications are supported in web apps saved to the home screen.
options.actions parameter
Experimental
Chrome – Full support
Chrome 48 (Release date: 2016-01-20)
footnote Full support
Edge – Full support
Edge 18 (Release date: 2018-10-02)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 35 (Release date: 2016-02-02)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 48 (Release date: 2016-01-26)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 35 (Release date: 2016-02-04)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 5 (Release date: 2016-12-15)
footnote Full support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – No support
WebView on iOS
footnote No support
options.badge parameter
Chrome – Full support
Chrome 53 (Release date: 2016-08-31)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 39 (Release date: 2016-08-02)
footnote Full support
Safari – No support
Safari
footnote
footnote See bug 280160
Chrome Android – Full support
Chrome Android 53 (Release date: 2016-09-07)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 41 (Release date: 2016-10-25)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote
footnote See bug 280160
Samsung Internet – Full support
Samsung Internet 6 (Release date: 2017-08-23)
footnote Full support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – No support
WebView on iOS
footnote
footnote See bug 280160
options.data parameter
Experimental
Chrome – Full support
Chrome 44 (Release date: 2015-07-21)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 31 (Release date: 2015-08-04)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 44 (Release date: 2015-07-29)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 32 (Release date: 2015-09-23)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 4 (Release date: 2016-03-11)
footnote Full support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – No support
WebView on iOS
footnote No support
options.image parameter
Experimental
Chrome – Full support
Chrome 56 (Release date: 2017-01-25)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 43 (Release date: 2017-02-07)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 56 (Release date: 2017-02-01)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 43 (Release date: 2017-09-27)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 6 (Release date: 2017-08-23)
footnote Full support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – No support
WebView on iOS
footnote No support
options.renotify parameter
Experimental
Chrome – Full support
Chrome 50 (Release date: 2016-04-13)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 37 (Release date: 2016-05-04)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 50 (Release date: 2016-04-13)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 37 (Release date: 2016-06-16)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 5 (Release date: 2016-12-15)
footnote Full support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – No support
WebView on iOS
footnote No support
options.requireInteraction parameter
Experimental
Chrome – Full support
Chrome 47 (Release date: 2015-12-01)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 34 (Release date: 2015-12-08)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 47 (Release date: 2015-12-02)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 34 (Release date: 2015-12-16)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 5 (Release date: 2016-12-15)
footnote Full support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – No support
WebView on iOS
footnote No support
options.vibrate parameter
Experimental
Chrome – Full support
Chrome 45 (Release date: 2015-09-01)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 32 (Release date: 2015-09-15)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – No support
Chrome Android
footnote
footnote In Android Oreo and above, regardless of Chrome version, this parameter has no effect. See bug 40630890.
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – No support
Opera Android
footnote
footnote In Android Oreo and above, regardless of Chrome version, this parameter has no effect. See bug 40630890.
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – No support
Samsung Internet
footnote
footnote In Android Oreo and above, regardless of Chrome version, this parameter has no effect. See bug 40630890.
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – No support
WebView on iOS
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.
See implementation notes.