Notifications API

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 Notifications API allows web pages to control the display of system notifications to the end user. These are outside the top-level browsing context viewport, so therefore can be displayed even when the user has switched tabs or moved to a different app. The API is designed to be compatible with existing notification systems, across different platforms.

Concepts and usage

On supported platforms, showing a system notification generally involves two things. First, the user needs to grant the current origin permission to display system notifications, which is generally done when the app or site initializes, using the Notification.requestPermission() method. This method should only be called when handling a user gesture, such as when handling a mouse click. For example:

js
btn.addEventListener("click", () => {
  let promise = Notification.requestPermission();
  // wait for permission
});

This will spawn a request dialog, along the following lines:

A dialog box asking the user to allow notifications from that origin. There are options to never allow or allow notifications.

From here the user can choose to allow notifications from this origin, or block them. Once a choice has been made, the setting will generally persist for the current session.

Next, a new notification is created using the Notification() constructor. This must be passed a title argument, and can optionally be passed an options object to specify options, such as text direction, body text, icon to display, notification sound to play, and more.

In addition, the Notifications API spec specifies a number of additions to the ServiceWorker API, to allow service workers to fire notifications.

Note: To find out more about using notifications in your own app, read Using the Notifications API.

Interfaces

Notification

Defines a notification object.

NotificationEvent

Represents a notification event dispatched on the ServiceWorkerGlobalScope of a ServiceWorker.

Extensions to other interfaces

notificationclick event

Occurs when a user clicks on a displayed notification.

notificationclose event

Occurs when a user closes a displayed notification.

ServiceWorkerRegistration.getNotifications()

Returns a list of the notifications in the order that they were created from the current origin via the current service worker registration.

ServiceWorkerRegistration.showNotification()

Displays the notification with the requested title.

Specifications

Specification
Notifications API

Browser compatibility

api.Notification

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Notification
Notification() constructor
actions
Experimental
badge
body
click event
close
close event
data
dir
error event
icon
image
Experimental
lang
maxActions static property
Experimental
permission static property
renotify
Experimental
requestPermission() static method
Returns a Promise
requireInteraction
Secure context required
show event
silent
tag
timestamp
Experimental
title
vibrate
Experimental
Available in workers

Legend

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

Full support
Full support
Partial support
Partial support
No support
No support
Experimental. Expect behavior to change in the future.
See implementation notes.
User must explicitly enable this feature.
Requires a vendor prefix or different name for use.
Has more compatibility info.

api.ServiceWorkerRegistration.showNotification

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
showNotification
options.actions parameter
Experimental
options.badge parameter
options.data parameter
Experimental
options.image parameter
Experimental
options.renotify parameter
Experimental
options.requireInteraction parameter
Experimental
options.vibrate parameter
Experimental

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.

api.ServiceWorkerRegistration.getNotifications

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
getNotifications

Legend

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

Full support
Full support
No support
No support
See implementation notes.

See also