Notification.requestPermission()
有 3 种可能的结果:granted
、denied
和 default
。
在 Chrome 中,当用户使用 X 关闭权限对话框而不是明确地说出 block
时,您会得到 default
。但是,如果在获得 default
作为结果后,您调用 Notification.permission
,您将得到 denied
,使得以后无法再次尝试请求许可。
这是设计的吗?有没有办法让 chrome 区别对待这两个结果? Firefox 以正确的方式处理这个问题(您可以询问权限,直到用户明确拒绝)
最佳答案
我会留下这个以防万一有人在寻找答案:
当用户第三次关闭权限对话框时,Chrome 会自动将权限设置为拒绝
(它会在下面的权限弹出窗口中显示一条自动阻止
消息导航栏)。因此,用户关闭对话框的前三次结果是 default
,但在第三次时,权限设置为 denied
。
我使用这种逻辑的方式是:
window.Notification.requestPermission().then((result) => {
if (result === 'denied') {
// the user has denied permission
return;
}
if (result === 'default') {
// the user has closed the dialog
if (window.Notification.permission === 'denied') {
// the browser has decided to automatically denied permission
}
return;
}
// the user has granted permission
});
关于javascript - Chrome 将 Notification.requestPermission() 的结果 'default' 视为 'denied',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47263967/