Secure context
This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The Notification interface of the Notifications API is used to configure and display desktop notifications to the user. These notifications' appearance and specific functionality vary across platforms but generally they provide a way to asynchronously provide information to the user.
Constructor
Notification()- Creates a new instance of the
Notificationobject.
Properties
Static properties
These properties are available only on the Notification object itself.
Notification.permissionRead only- A string representing the current permission to display notifications. Possible values are:
denied— The user refuses to have notifications displayed.granted— The user accepts having notifications displayed.default— The user choice is unknown and therefore the browser will act as if the value were denied.
Notification.maxActionsRead only
Instance properties
These properties are available only on instances of the Notification object.
Notification.actionsRead only- The actions array of the notification as specified in the constructor's
optionsparameter. Notification.badgeRead only- The URL of the image used to represent the notification when there is not enough space to display the notification itself.
Notification.bodyRead only- The body string of the notification as specified in the constructor's
optionsparameter. Notification.dataRead only- Returns a structured clone of the notification’s data.
Notification.dirRead only- The text direction of the notification as specified in the constructor's
optionsparameter. Notification.langRead only- The language code of the notification as specified in the constructor's
optionsparameter. Notification.tagRead only- The ID of the notification (if any) as specified in the constructor's
optionsparameter. Notification.iconRead only- The URL of the image used as an icon of the notification as specified in the constructor's
optionsparameter. Notification.imageRead only- The URL of an image to be displayed as part of the notification, as specified in the constructor's
optionsparameter. Notification.renotifyRead only- Specifies whether the user should be notified after a new notification replaces an old one.
Notification.requireInteractionRead only- A
Booleanindicating that a notification should remain active until the user clicks or dismisses it, rather than closing automatically. Notification.silentRead only- Specifies whether the notification should be silent — i.e., no sounds or vibrations should be issued, regardless of the device settings.
Notification.timestampRead only- Specifies the time at which a notification is created or applicable (past, present, or future).
Notification.titleRead only- The title of the notification as specified in the first parameter of the constructor.
Notification.vibrateRead only- Specifies a vibration pattern for devices with vibration hardware to emit.
Event handlers
Notification.onclick- A handler for the
clickevent. It is triggered each time the user clicks on the notification. Notification.onclose- A handler for the
closeevent. It is triggered when the user closes the notification. Notification.onerror- A handler for the
errorevent. It is triggered each time the notification encounters an error. Notification.onshow- A handler for the
showevent. It is triggered when the notification is displayed.
Methods
Static methods
These methods are available only on the Notification object itself.
Notification.requestPermission()- Requests permission from the user to display notifications.
Instance methods
These properties are available only on an instance of the Notification object or through its prototype. The Notification object also inherits from the EventTarget interface.
Notification.close()- Programmatically closes a notification instance.
Examples
Assume this basic HTML:
<button onclick="notifyMe()">Notify me!</button>
It's possible to send a notification as follows — here we present a fairly verbose and complete set of code you could use if you wanted to first check whether notifications are supported, then check if permission has been granted for the current origin to send notifications, then request permission if required, before then sending a notification.
function notifyMe() {
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}
// Let's check whether notification permissions have already been granted
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
var notification = new Notification("Hi there!");
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== "denied") {
Notification.requestPermission().then(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification("Hi there!");
}
});
}
// At last, if the user has denied notifications, and you
// want to be respectful there is no need to bother them any more.
}
We no longer show a live sample on this page, as Chrome and Firefox no longer allow notification permissions to be requested from cross-origin <iframe>s, with other browsers to follow. To see a example in action, check out our To-do list example (also see the app running live.)
Note: In the above example we spawn notifications in response to a user gesture (clicking a button). 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.
Specifications
| Specification | Status | Comment |
|---|---|---|
| Notifications API | Living Standard | Living standard |
Browser compatibility
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
Notification | Chrome
Full support
22
| Edge Full support 14 | Firefox
Full support
22
| IE No support No | Opera Full support 25 | Safari Full support 6 | WebView Android No support No | Chrome Android Full support Yes | Firefox Android
Full support
22
| Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes |
Notification() constructor | Chrome
Full support
22
| Edge Full support ≤18 | Firefox
Full support
22
| IE No support No | Opera Full support 25 | Safari Full support 6 | WebView Android No support No | Chrome Android Full support Yes | Firefox Android
Full support
22
| Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes |
actions | Chrome Full support 53 | Edge Full support 18 | Firefox No support No | IE No support No | Opera Full support 39 | Safari ? | WebView Android No support No | Chrome Android Full support 53 | Firefox Android No support No | Opera Android Full support 41 | Safari iOS No support No | Samsung Internet Android Full support 6.0 |
badge | Chrome Full support 53 | Edge Full support 18 | Firefox No support No | IE No support No | Opera Full support 39 | Safari ? | WebView Android No support No | Chrome Android Full support 53 | Firefox Android No support No | Opera Android Full support 41 | Safari iOS No support No | Samsung Internet Android Full support 6.0 |
body | Chrome Full support Yes | Edge Full support 14 | Firefox Full support Yes | IE No support No | Opera Full support Yes | Safari Full support Yes | WebView Android No support No | Chrome Android Full support Yes | Firefox Android Full support Yes | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes |
close | Chrome Full support Yes | Edge Full support 14 | Firefox Full support Yes | IE No support No | Opera Full support Yes | Safari Full support Yes | WebView Android No support No | Chrome Android Full support Yes | Firefox Android Full support Yes | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes |
data | Chrome Full support Yes | Edge Full support 16 | Firefox Full support Yes | IE No support No | Opera Full support Yes | Safari ? | WebView Android No support No | Chrome Android Full support Yes | Firefox Android Full support Yes | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes |
dir | Chrome Full support Yes | Edge Full support 14 | Firefox Full support Yes | IE No support No | Opera Full support Yes | Safari Full support Yes | WebView Android No support No | Chrome Android Full support Yes | Firefox Android Full support Yes | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes |
icon | Chrome
Full support
22
| Edge Full support 14 | Firefox
Full support
22
| IE No support No | Opera Full support 25 | Safari No support No | WebView Android No support No | Chrome Android Full support Yes | Firefox Android
Full support
22
| Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes |
image | Chrome Full support 53 | Edge Full support 18 | Firefox No support No | IE No support No | Opera Full support 40 | Safari ? | WebView Android No support No | Chrome Android Full support 53 | Firefox Android No support No | Opera Android Full support 41 | Safari iOS No support No | Samsung Internet Android Full support 6.0 |
lang | Chrome Full support Yes | Edge Full support 14 | Firefox Full support Yes | IE No support No | Opera Full support Yes | Safari Full support Yes | WebView Android No support No | Chrome Android Full support Yes | Firefox Android Full support Yes | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes |
maxActions | Chrome Full support Yes | Edge Full support 18 | Firefox No support No | IE No support No | Opera Full support Yes | Safari ? | WebView Android No support No | Chrome Android Full support Yes | Firefox Android No support No | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes |
onclick | Chrome Full support Yes | Edge Full support 14 | Firefox Full support 22 | IE No support No | Opera Full support Yes | Safari Full support Yes | WebView Android No support No | Chrome Android Full support Yes | Firefox Android No support No | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes |
onclose | Chrome Full support Yes | Edge Full support 14 | Firefox Full support Yes | IE No support No | Opera Full support Yes | Safari Full support Yes | WebView Android No support No | Chrome Android Full support Yes | Firefox Android Full support Yes | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes |
onerror | Chrome Full support Yes | Edge Full support 14 | Firefox No support No | IE No support No | Opera Full support Yes | Safari Full support Yes | WebView Android No support No | Chrome Android Full support Yes | Firefox Android No support No | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes |
onshow | Chrome Full support Yes | Edge Full support 14 | Firefox Full support Yes | IE No support No | Opera Full support Yes | Safari Full support Yes | WebView Android No support No | Chrome Android Full support Yes | Firefox Android Full support Yes | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes |
permission | Chrome Full support Yes | Edge Full support 14 | Firefox Full support Yes | IE No support No | Opera Full support Yes | Safari Full support Yes | WebView Android No support No | Chrome Android Full support Yes | Firefox Android Full support Yes | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes |
renotify | Chrome Full support 50 | Edge Full support 79 | Firefox No support No | IE No support No | Opera Full support 37 | Safari No support No | WebView Android No support No | Chrome Android Full support 50 | Firefox Android No support No | Opera Android Full support 37 | Safari iOS No support No | Samsung Internet Android Full support 5.0 |
requestPermission | Chrome Full support 46 | Edge Full support 14 | Firefox
Full support
47
| IE No support No | Opera Full support 40 | Safari Full support Yes | WebView Android No support No | Chrome Android Full support 46 | Firefox Android Full support Yes | Opera Android Full support 41 | Safari iOS No support No | Samsung Internet Android Full support 5.0 |
requireInteraction | Chrome Full support Yes | Edge Full support 17 | Firefox No support No | IE No support No | Opera Full support Yes | Safari ? | WebView Android No support No | Chrome Android Full support Yes | Firefox Android No support No | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes |
| Secure context required | Chrome Full support 62 | Edge Full support ≤79 | Firefox Full support 67 | IE No support No | Opera Full support 49 | Safari ? | WebView Android No support No | Chrome Android Full support 62 | Firefox Android Full support 67 | Opera Android Full support 46 | Safari iOS No support No | Samsung Internet Android Full support 8.0 |
silent | Chrome Full support 43 | Edge Full support 17 | Firefox No support No | IE No support No | Opera Full support 30 | Safari No support No | WebView Android No support No | Chrome Android Full support 43 | Firefox Android No support No | Opera Android Full support 30 | Safari iOS No support No | Samsung Internet Android Full support 4.0 |
tag | Chrome Full support Yes | Edge Full support 14 | Firefox Full support Yes | IE No support No | Opera Full support Yes | Safari Full support Yes | WebView Android No support No | Chrome Android Full support Yes | Firefox Android Full support Yes | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes |
timestamp | Chrome Full support Yes | Edge Full support 17 | Firefox No support No | IE No support No | Opera Full support Yes | Safari ? | WebView Android No support No | Chrome Android Full support Yes | Firefox Android No support No | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes |
title | Chrome Full support Yes | Edge Full support 14 | Firefox No support No | IE No support No | Opera Full support Yes | Safari Full support Yes | WebView Android No support No | Chrome Android Full support Yes | Firefox Android No support No | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes |
vibrate | Chrome No support No | Edge No support No | Firefox No support No | IE No support No | Opera No support No | Safari ? | WebView Android No support No | Chrome Android
Full support
53
| Firefox Android No support No | Opera Android
Full support
41
| Safari iOS No support No | Samsung Internet Android
Full support
6.0
|
| Available in workers | Chrome Full support 45 | Edge Full support ≤18 | Firefox Full support 41 | IE No support No | Opera Full support 32 | Safari ? | WebView Android No support No | Chrome Android Full support 45 | Firefox Android Full support 41 | Opera Android Full support 32 | Safari iOS No support No | Samsung Internet Android Full support 5.0 |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- See implementation notes.
- See implementation notes.
- Requires a vendor prefix or different name for use.
- Requires a vendor prefix or different name for use.
