javascript - Chrome 将 Notification.requestPermission() 的结果 'default' 视为 'denied'

标签 javascript google-chrome push-notification permissions

Notification.requestPermission() 有 3 种可能的结果:granteddenieddefault

在 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/

相关文章:

javascript - 如何在 submitHandler 回调中访问 Backbone View ?

javascript - div 内的动态 css 圆圈

javascript - 如何将多数组参数传递给 jquery for 循环

css - 为什么我的页面在 Firefox 和 IE 11 中看起来不对?

iOS 推送通知 .p12 证书

ios - 使用 PushNotificationIOS 时没有已知的选择器类方法

javascript - 将对象与数组进行比较

javascript - 如何使用 Visual Studio(如 IE)在 Chrome 中获得完整的 JavaScript/TypeScript 调试

javascript - three.js 纹理错误读取 "GL_INVALID_OPERATION : glDrawElements: Source and destination textures of the draw are the same"

ios - 使用 UIwebview 应用程序从设备获取 UID 以获取苹果推送通知