You’re reading the English version of this content since no translation exists yet for this locale. Help us translate this article!
Secure context
This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
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
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);
Safari Version 12.0.3 still uses callback to get the permission.
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
, or default
.
Example
The following snippet requests permission from the user, then logs a different result to the console depending on the user's 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. });
Specifications
Specification | Status | Comment |
---|---|---|
Notifications API | Living Standard | Living standard |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
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 Yes |
Legend
- Full support
- Full support
- No support
- No support