可以使用 Notification.permission
属性检查浏览器通知的权限。
在 firefox 中,用户可以暂时禁用浏览器通知。这意味着当打开新标签页或窗口时,浏览器将请求再次显示通知的权限。
当用户临时拒绝权限并且我检查 Notification.permission
属性时,它返回 default
。
当我运行以下脚本时,我在控制台中得到允许的浏览器通知
作为输出。
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:我打开了 an issue在 WHATWG
的通知存储库中。
编辑 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?
只有当您询问用户两次并且他们已经在同一 session 中说“不”一次时,Firefox 才会出现临时阻止。执行页面刷新 ↻ 的用户应该删除该 block 。此时,Firefox 正在保护用户免受骚扰网站的侵害。
如果您还没有询问过他们,default
意味着系统会提示用户。弄清楚你是否已经拥有,希望很容易,如果你拥有,不要再打扰他们。
关于javascript - 检查浏览器通知是否在 firefox 中被临时禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49490817/