Notification:permission 静态属性
注意:此功能在Web Workers中可用。
permission
是 Notification
接口的只读静态属性,它指示用户为当前来源显示 Web 通知授予的当前权限。
值
示例
如果您想首先检查是否支持通知,然后检查是否已为当前来源授予发送通知的权限,然后在发送通知之前根据需要请求权限,则可以使用以下代码段。
js
function notifyMe() {
if (!("Notification" in window)) {
// Check if the browser supports notifications
alert("This browser does not support desktop notification");
} else if (Notification.permission === "granted") {
// Check whether notification permissions have already been granted;
// if so, create a notification
const notification = new Notification("Hi there!");
// …
} else if (Notification.permission !== "denied") {
// We need to ask the user for permission
Notification.requestPermission().then((permission) => {
// If the user accepts, let's create a notification
if (permission === "granted") {
const 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 anymore.
}
规范
规范 |
---|
Notifications API 标准 # dom-notification-permission |
浏览器兼容性
BCD 表格仅在启用 JavaScript 的浏览器中加载。