通知弹出窗口未显示在Chrome中 [英] Notification popup doesn't appear in chrome

查看:573
本文介绍了通知弹出窗口未显示在Chrome中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行类似Gmail的桌面通知.我面临在chrome中更改通知权限的困难.如果我使用 window.Notification.permission ,则权限始终在控制台中显示为拒绝.如果我在谷歌浏览器设置"->隐私"->内容设置"->通知"中手动更改了从不允许所有站点显示桌面通知"中的允许所有站点显示桌面通知".现在,我可以正常获取桌面通知了.但是,如果浏览器将其设置为不允许任何站点显示桌面通知",那么我需要发出警报以询问权限,然后我需要从弹出窗口中选择允许",以将设置更改为允许所有站点显示桌面通知".通知".问题是,即使我未收到许可警报,如果我这样做,许可也不会更改.权限检查脚本如下

I am trying to do a gmail similar type desktop notification . I am facing the difficulty to change the notification permission in chrome. The permission always shows denied in console if i use window.Notification.permission. If i manually change the permission in google chrome settings -> privacy -> content settings -> Notifications as "Allow all sites to show desktop notifications" from "Do not allow any site to show desktop notifications" . Now i am able to get desktop notification normally. But I need an alert to asking a permission if the browser have setting as "Do not allow any site to show desktop notifications" , then i need to choose allow from the popup in order to change the setting as "Allow all sites to show desktop notifications". The problem is permission is not changed if i do like this even the permission alert is not coming . The permission checking script follows

if(Notification.permission == 'denied'){
                 Notification.requestPermission(function (status){
                        console.log("Reaching here");
                        Notification.permission = status;
                     });
            }

未发生用于请求允许或禁止通知的弹出窗口.在此先感谢您为我提供解决方案.

The popup for requesting permission to allow or disallow notifications is not occurred. Thanks in advance for suggesting me a solution.

推荐答案

您的脚本似乎有问题.

There seem to be a problem with your script.

这是标准的一部分,当permission设置为denied时,您永远不能显示询问您是否允许...发送桌面通知?"的弹出窗口.仅当permission设置为default时才使用此弹出窗口,这实际上意味着用户不在乎,您应该问他是否想要它们.

It's part of the standard that when the permission is set to denied you can NEVER show the popup which asks "Do you want to allow... to send desktop notifications ?". This popup is used only in the case the permission is set to default, which in fact means that the user doesn't care and you should ask him if he wants them or not.

这是我使用的条件:

Notification['permission'] !== 'granted' && Notification['permission'] !== 'denied'

因为并非所有浏览器都支持default值.加上permission属性不是在chrome 32之前实现的,这就是为什么您应该使用方括号访问它的原因.

Because the default value isn't supported by all browsers. Plus the permission attribute was not implemented prior to chrome 32, that's why you should access it using the square brackets.

实际上,您也可以删除我的条件语句中的denied部分,因为如果权限被拒绝,它将不会执行任何操作.您可以参考此MDN文档以获取更多信息有关兼容性等问题.

In fact you could also remove the denied part in my conditionnal, because it won't do anything if the permission is denied. You can refer to this MDN documentation to get more information on compatibility and things like that.

这篇关于通知弹出窗口未显示在Chrome中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆