The requestPermission() method of the Notification interface requests permission from the user for the current origin to display notifications.
Note: This feature is not available in SharedWorker
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);
callback Optional Deprecated since Gecko 46
A Promise that resolves to a DOMString with the permission picked by the user. Possible values for this string are granted, denied, or default.
The following snippet requests permission from the user, then logs a different result to the console depending on the users' choice.
Notification.requestPermission().then(function(result) {
if (result === 'denied') {
console.log('Permission wasn\'t granted. Allow a retry.');
return;
}
if (result === 'default') {
console.log('The permission request was dismissed.');
return;
}
// Do something with the granted permission.
}); | Specification | Status | Comment |
|---|---|---|
| Notifications API | Living Standard | Living standard |
| Desktop | ||||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
| Basic support | 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 | 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/requestPermission