重置被拒绝的HTML通知

fir*_*ash 2 html javascript google-chrome web-notifications

我有一个Web应用程序,我在其中使用HTML通知.如果用户第一次允许它并开始使用它,它工作正常,但是如果用户第一次通过单击阻止按钮阻止通知,稍后尝试通过某些用户手势再次请求权限,则浏览器不会触发(允许/阻止)弹出窗口.

这是我第二次触发许可.

if(Notification.permission == 'denied' || Notification.permission == 'default'){

        Notification.requestPermission(function (permission) {
    // If the user accepts, let's create a notification
            if (permission === "granted") {
                console.log("Regranted");
            }
        });
    }
Run Code Online (Sandbox Code Playgroud)

它适用于default案件,但不适用于denied案件.

sid*_*ker 6

正如之前的评论指出的那样,你所看到的行为是设计的.如果您阅读https://notifications.spec.whatwg.org/#dom-notification-requestpermission上的步骤2,子步骤2,您将看到规范要求用​​户被询问是否接受显示通知的唯一时间是权限值是default.如果权限值为grantedblocked,则该算法要求再次询问用户是否可接受显示通知.

如果用户对他们阻止的网站的通知有所改变,可以选择进入他们的浏览器设置并自行更改该网站的权限设置.


小智 6

我建议有一个按钮来打开通知,然后检查那里的权限,如果之前被拒绝,则退回。

前任:

  if (Notification.permission === "denied") {
    alert("Notifications blocked. Please enable them in your browser.");
  }
Run Code Online (Sandbox Code Playgroud)