W3cubDocs

/Web APIs

Notifications API

Note: This feature is available in Web Workers

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

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 should be done in response to a user gesture, such as clicking a button, for example:

js

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

This is not only best practice — you should not be spamming users with notifications they didn't agree to — but going forward browsers will explicitly disallow notifications not triggered in response to a user gesture. Firefox is already doing this from version 72, for example.

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.

Note: As of Firefox 44, the permissions for Notifications and Push have been merged. If permission is granted for notifications, push will also be enabled.

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.

Notifications interfaces

Notification

Defines a notification object.

Service worker additions

ServiceWorkerRegistration

Includes the ServiceWorkerRegistration.showNotification() and ServiceWorkerRegistration.getNotifications() method, for controlling the display of notifications.

ServiceWorkerGlobalScope

Includes the onnotificationclick handler, for firing custom functions when a notification is clicked.

NotificationEvent

A specific type of event object, based on ExtendableEvent, which represents a notification that has fired.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
Notification 20 14 224 No 23 7 No No 224 No 16.4 No
Notifications_API
20["Starting in Chrome 49, notifications do not work in incognito mode.", "Before Chrome 42, service worker additions were not supported."]
14 224 No
23["Starting in Opera 36, notifications do not work in incognito mode.", "Before Opera 29, service worker additions were not supported."]
7 No
42["Notifications in Chrome for Android are only available through service workers. To show notifications on Android, see ServiceWorkerRegistration.showNotification().", "Starting in Chrome 49, notifications do not work in incognito mode."]
224
29["Notifications in Opera for Android are only available through service workers. To show notifications on Android, see ServiceWorkerRegistration.showNotification().", "Starting in Opera for Android 36, notifications do not work in incognito mode."]
16.4
4.0["Notifications in Samsung Internet are only available through service workers. To show notifications on Android, see ServiceWorkerRegistration.showNotification().", "Starting in Samsung Internet 5.0, notifications do not work in incognito mode."]
actions 53 18 No No 39 No No 53 No 41 No 6.0
badge 53 18 No No 39
17Badging is supported for web apps saved to the Dock in Safari 17 on the macOS Sonoma beta
No 53 No 41 16.4 6.0
body 33 14 26 No 23 11 No 42 26 29 16.4 4.0
click_event 20 14 22 No 23 7 No 42 22 29 No 4.0
close 20 14 22 No 23 7 No 42 22 29 16.4 4.0
close_event 20 14 22 No 23 7 No 42 22 29 No 4.0
data 44 16 34 No 31 16 No 44 34 32 16.4 4.0
dir 20 14 26 No 23 7 No 42 26 29 16.4 4.0
error_event 20 14 22 No 23 7 No 42 22 29 No 4.0
icon 33 14 26 No 23 11 No 42 26 29 16.4 4.0
image 56 18 No No 43 No No 56 No 43 No 6.0
lang 33 14 26 No 23 11 No 42 26 29 16.4 4.0
maxActions_static 48 18 No No 35 No No 48 No 35 No 5.0
permission_static 32 14 22 No 23 7 No 42 22 29 16.4 4.0
renotify 50 79 No No 37 No No 50 No 37 No 5.0
requestPermission_static 20 14
22["From Firefox 70 onwards, cannot be called from a cross-origin iframe.", "From Firefox 72 onwards, can only be called in response to a user gesture such as a click event."]
No 23 15
7–15Only supported the deprecated callback syntax.
No 42
22["From Firefox Android 79 onwards, cannot be called from a cross-origin iframe.", "From Firefox Android 79 onwards, can only be called in response to a user gesture such as a click event."]
29 16.4 4.0
requireInteraction 47 17
117Only supported on Windows. Behind a flag on other operating systems.
117
No 34 No No 47
117Only supported on Windows. Behind a flag on other operating systems.
34 No 5.0
secure_context_required 62 79 67 No 49 No No 62 67 46 No 8.0
show_event 20 14 22 No 23 7 No 42 22 29 No 4.0
silent 43 17 No No 30 16.6 No 43 No 30 16.6 4.0
tag 20 14 26 No 23 7 No 42 26 29
NoThis property is exposed but is not implemented. See WebKit bug 258922.
4.0
timestamp 50 17 No No 37 No No 50 No 37 No 5.0
title 33 14 26 No 23 11 No 42 26 29 16.4 4.0
vibrate 53 79 No No 40 No No
53Does not work on Android O or later regardless of Chrome version.
No
41Does not work on Android O or later regardless of Chrome version.
No
6.0Does not work on Android O or later regardless of Chrome version.
worker_support 42 15 41 No 29 No No 42 41 29 No 4.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API