W3cubDocs

/DOM

Notifications API

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 initialises, using the Notification.requestPermission() method. This will spawn a request dialog, along the following lines:

From here the user can choose to allow notifications from this origin, block notifications from this origin, or not choose at this point. 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.

Note: This feature is available in Web Workers.

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 ServiceWorkerGlobalScope.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

Specification Status Comment
Notifications API Living Standard Living standard

Browser compatibilityUpdate compatibility data on GitHub

Notification

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Basic support 22
22
Before Chrome 22, the support for notification followed an old prefixed version of the specification and used the navigator.webkitNotifications object to instantiate a new notification. Before Chrome 32, Notification.permission was not supported. Before Chrome 42, service worker additions were not supported. Starting in Chrome 49, notifications do not work in incognito mode.
5
Prefixed
Prefixed Requires the vendor prefix: -webkit-
Yes 22
22
4
Prefixed
Prefixed Requires the vendor prefix: -moz-
No 25 6
Available in workers 45 Yes 41 No 32 ?
Secure contexts only 62 ? ? No 49 ?
Notification() constructor 22
22
5
Prefixed
Prefixed Requires the vendor prefix: webkit
Yes 22
22
4
Prefixed
Prefixed Requires the vendor prefix: moz
No 25 6
actions 53 18 No No 39 ?
badge 53 18 No No 39 ?
body Yes ? Yes No ? ?
data Yes ? Yes No ? ?
dir Yes ? Yes No ? ?
icon 22
22
5
Prefixed
Prefixed Requires the vendor prefix: -webkit-
? 22
22
4
Prefixed
Prefixed Requires the vendor prefix: -moz-
No 25 No
image 53 18 No No 40 ?
lang Yes ? Yes No ? ?
maxActions Yes 18 No No ? ?
onclick Yes ? No No ? ?
onclose Yes ? Yes No ? ?
onerror Yes ? No No ? ?
onshow Yes ? Yes No ? ?
permission Yes ? Yes No ? ?
renotify 50 No No No 37 No
requireInteraction Yes 17 No No ? ?
silent 43 17 No No 30 No
tag Yes ? Yes No ? ?
timestamp Yes 17 No No ? ?
title Yes ? No No ? ?
vibrate 53 No No No 39 ?
close Yes ? Yes No ? ?
requestPermission 46 ? 47 No 40 ?
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
Basic support No Yes ? 22
22
4
Prefixed
Prefixed Requires the vendor prefix: -webkit-
Yes No ?
Available in workers No 45 Yes 41 32 No ?
Secure contexts only No 62 ? ? 49 No ?
Notification() constructor No Yes ? 22
22
4
Prefixed
Prefixed Requires the vendor prefix: moz
Yes No ?
actions No 53 No No 39 No ?
badge No 53 No No 39 No ?
body No Yes ? Yes ? No ?
data No Yes ? Yes ? No ?
dir No Yes ? Yes ? No ?
icon No Yes ? 22
22
4
Prefixed
Prefixed Requires the vendor prefix: -moz-
Yes No ?
image No 53 ? No 40 No ?
lang No Yes ? Yes ? No ?
maxActions No Yes ? No ? No ?
onclick No Yes ? No ? No ?
onclose No Yes ? Yes ? No ?
onerror No Yes ? No ? No ?
onshow No Yes ? Yes ? No ?
permission No Yes ? Yes ? No ?
renotify No 50 No No 37 No ?
requireInteraction No Yes 17 No ? No ?
silent No 43 17 No 30 No ?
tag No Yes ? Yes ? No ?
timestamp No Yes 17 No ? No ?
title No Yes ? No ? No ?
vibrate No 53 No No 39 No ?
close No Yes ? Yes ? No ?
requestPermission No 46 ? Yes 40 No ?

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API