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.
Notification()Notification object.These properties are available only on the Notification object itself.
Notification.permission Read only
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.These properties are available only on instances of the Notification object.
Notification.actions Read only
options parameter.Notification.badge Read only
Notification.body Read only
options parameter.Notification.data Read only
Notification.dir Read only
options parameter.Notification.lang Read only
options parameter.Notification.tag Read only
options parameter.Notification.icon Read only
options parameter.Notification.image Read only
options parameter.Notification.renotify Read only
Notification.requireInteraction Read only
Boolean indicating that a notification should remain active until the user clicks or dismisses it, rather than closing automatically.Notification.silent Read only
Notification.timestamp Read only
Notification.title Read only
Notification.vibrate Read only
Notification.onclickclick event. It is triggered each time the user clicks on the notification.Notification.oncloseclose event. It is triggered when the user closes the notification.Notification.onerrorerror event. It is triggered each time the notification encounters an error.Notification.onshowshow event. It is triggered when the notification is displayed.These methods are available only on the Notification object itself.
Notification.requestPermission()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()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.
} In many cases, you don't need to be this verbose. For example, in our Emogotchi demo (see source code), we simply run Notification.requestPermission regardless to make sure we can get permission to send notifications (this uses the newer promise-based method syntax):
Notification.requestPermission().then(function(result) {
console.log(result);
}); Then we run a simple spawnNotification() function when we want to fire a notification — this is passed arguments to specify the body, icon, and title we want. Then it creates the necessary options object and fires the notification using the Notification() constructor.
function spawnNotification(body, icon, title) {
var options = {
body: body,
icon: icon
};
var n = new Notification(title, options);
} | Specification | Status | Comment |
|---|---|---|
| Notifications API | Living Standard | Living standard |
| Desktop | ||||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
| Basic support | 22
|
Yes | 22
|
No | 25 | 6 |
| Available in workers | 45 | Yes | 41 | No | 32 | ? |
| Secure contexts only | 62 | ? | ? | No | 49 | ? |
Notification() constructor |
22
|
Yes | 22
|
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
|
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
|
Yes | No | ? |
| Available in workers | No | 45 | Yes | 41 | 32 | No | ? |
| Secure contexts only | No | 62 | ? | ? | 49 | No | ? |
Notification() constructor |
No | Yes | ? | 22
|
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
|
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 | ? |
© 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/Notification