【问题标题】:Chrome treats 'default' result from Notification.requestPermission() as 'denied'Chrome 将 Notification.requestPermission() 的“默认”结果视​​为“拒绝”
【发布时间】:2018-04-26 02:44:00
【问题描述】:

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

在 Chrome 中,当用户使用 X 关闭权限对话框而不是明确说出 block 时,您会得到 default。但是,如果在得到default 作为结果之后,你调用Notification.permission 你得到denied,这样以后就不可能再次重试请求权限。

这是设计使然吗?有没有办法让铬对这两个结果进行不同的处理? Firefox 以正确的方式处理这一点(您可以请求权限,直到用户明确拒绝)

【问题讨论】:

    标签: javascript google-chrome push-notification permissions


    【解决方案1】:

    我会留下这个以防万一有人在寻找答案:

    当用户第三次关闭权限对话框时,Chrome 会自动将权限设置为denied(它会在导航栏的权限弹出窗口下方显示automatically blocked 消息)。所以前三次用户关闭对话框你得到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
    });
    

    【讨论】:

    • 这非常罕见!我在想我错误地实现了一些东西
    • 在第三次之后将其设置为拒绝可能是有意义的,但对我来说奇怪的部分是 result === 'default' 而不是 denied 或其他东西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    相关资源
    最近更新 更多