The permission
read-only property of the Notification
interface indicates the current permission granted by the user for the current origin to display web notifications.
var permission = Notification.permission;
A DOMString
representing the current permission. The value can be:
granted
: The user has explicitly granted permission for the current origin to display system notifications.denied
: The user has explicitly denied permission for the current origin to display system notifications.default
: The user decision is unknown; in this case the application will act as if permission was denied
.The following fairly verbose snippet could be used 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)) { console.log("This browser does not support desktop notification"); } // Let's check whether notification permissions have alredy 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.permission === "default") { Notification.requestPermission(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. }
Specification | Status | Comment |
---|---|---|
Notifications API The definition of 'permission' in that specification. | Living Standard | Living standard |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes | ? | Yes | No | ? | ? |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | No | Yes | ? | Yes | ? | 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/permission