Note: This feature is available in Web Workers.
Secure contextThis feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Note: This feature is not available in SharedWorker
Note: Safari still uses the callback syntax to get the permission. Read Using the Notifications API for a good example of how to feature detect this and run code as appropriate.
The requestPermission()
method of the Notification
interface requests permission from the user for the current origin to display notifications.
Syntax
The latest spec has updated this method to a promise-based syntax that works like this:
Notification.requestPermission().then(function(permission) { ... });
Previously, the syntax was based on a simple callback; this version is now deprecated:
Notification.requestPermission(callback);
Parameters
callback
Optional Deprecated since Gecko 46- An optional callback function that is called with the permission value. Deprecated in favor of the promise return value.
Returns
A Promise
that resolves to a DOMString
with the permission picked by the user. Possible values for this string are:
granted
denied
default
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 [[../../../../../../../mdn.github.io/to-do-notifications/index|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
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
requestPermission
|
Chrome
Full support 46 |
Edge
Full support 14 |
Firefox Full support 47 Full support 47 Notes' From Firefox 70 onwards, cannot be called from a cross-origin IFrame.
Notes' From Firefox 72 onwards, can only be called in response to a user gesture such as a |
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 |
Legend
- Full support
- Full support
- No support
- No support
- See implementation notes.'
- See implementation notes.
See also
Notification.requestPermission() by Mozilla Contributors is licensed under CC-BY-SA 2.5.