检查浏览器通知是否在 firefox 中被临时禁用

Check if browser notifications are temporary disabled in firefox

可以通过Notification.permission 属性检查浏览器通知的权限。

在 firefox 中,用户可以暂时禁用浏览器通知。这意味着当打开新标签或 window 时,浏览器将请求再次显示通知的权限。

当用户临时拒绝权限时我检查Notification.permission 属性它returns default.

当我 运行 以下脚本时,我在控制台中得到 browser notifications where allowed 作为输出。

Notification.requestPermission().then((result) => {
    console.log(result);
});

但是当我尝试显示通知时没有任何反应(因为它们是临时禁用的)。


How can I check if browser notifications has been temporary disabled in firefox?

我可以使用以下脚本,但这是一个丑陋的解决方法,它会触发浏览器请求权限的对话框。我可以做点别的吗?

var calculateBrowserNotificationPermission = () => {
    return new Promise(function(resolve, reject) {
        var permission = Notification.permission;
        if(permission !== 'default') {
            resolve(permission);
            return;
        }

        Notification.requestPermission().then((result) => {
            if(result === 'default') {
                resolve('temporary_denied');       
            } else {
                resolve(result);
            }
        });
    });
};

如果尚不存在这样的功能,最好在哪里请求?

编辑 1:我在 WHATWG 的通知存储库中打开了 an issue

编辑 2:我已经关闭了 github 问题,如果我已经按照@jib 的建议向用户请求权限,我将继续存在。

But then when I try to show notifications nothing happens

您使用的 API 有误。 requestPermission 拒绝拒绝,结果为:

(async () => {
  try {
    console.log(await Notification.requestPermission()); // denied
  } catch (e) {
    console.log(e); // never gets here
  }
})();

此处除 granted 以外的任何结果都表示您未获得许可。试试看 here.

  • granted 表示您获得了执行通知的持久权限。
  • denied 表示用户选择永久屏蔽您。
  • default 表示用户选择不允许这次(在 Firefox 中启动临时阻止)。

How can I check if browser notifications has been temporary disabled in firefox?

只有当您询问用户两次并且他们已经在同一个会话中说了一次 "no" 时,Firefox 才会出现临时阻止。执行页面刷新 ↻ 的用户应该删除该块。此时 Firefox 正在保护用户免受骚扰 web-sites。

如果您还没有询问过他们,default 意味着系统会提示用户。弄清楚你是否已经拥有,希望很容易,如果你拥有,就不要再打扰他们了。