Notification:permission 静态属性
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
备注: 此特性在 Web Worker 中可用。
Notification
接口的 permission
只读静态属性表明当前用户是否授予当前来源(origin)显示 web 通知的权限。
值
示例
如果你想首先检查是否支持通知,然后检查是否已授予当前来源发送通知的权限,然后在发送通知之前请求权限(如果需要),则可以使用以下代码段。
js
function notifyMe() {
if (!("Notification" in window)) {
// 检查浏览器是否支持桌面通知
alert("此浏览器不支持桌面通知");
} else if (Notification.permission === "granted") {
// 检查通知权限是否已经被授予;如果是的话,创建一条通知
const notification = new Notification("你好呀!");
// …
} else if (Notification.permission !== "denied") {
// 我们需要请求用户的许可
Notification.requestPermission().then((permission) => {
// 如果用户同意,让我们创建一个通知
if (permission === "granted") {
const notification = new Notification("你好呀!");
// …
}
});
}
// 最后,如果用户拒绝了通知,而你想要尊重他们,就没有必要再打扰他们了
}
规范
Specification |
---|
Notifications API # dom-notification-permission |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
permission static property |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- Partial support
- Partial support
- No support
- No support
- See implementation notes.
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.