【问题标题】:Notification permission gives denied always通知权限总是被拒绝
【发布时间】:2017-11-23 04:37:02
【问题描述】:

我正在使用Notification.permission 来检查浏览器是否允许通知。

我的检查通知权限的代码如下。

    // Let's check if the browser supports notifications
    if (!("Notification" in window)) {
           alert("This browser does not support desktop notification");
    }
    var prm = Notification.permission;
    if (prm == 'default' || prm == 'denied') {
          console.log("permission denied or default");
    }else{
         console.log("permission granted");
    }

这段代码在我的localhost 中运行良好,但是当我尝试在生产中使用时,它总是会给出拒绝状态。 我的通知浏览器设置是在此站点上始终允许 但我不知道是什么问题。 需要帮助。

【问题讨论】:

  • it will always give denied status - 您的代码将“拒绝”和“默认”视为同一件事,您确定您被拒绝了吗?
  • 是的,如果 prm == 'default' || prm == 'denied' 我想打印 console.log("permission denied or default");但 prm 在生产中总是给出拒绝状态,但在 localhost 中完美运行。
  • 根据图片,您的通知设置为Ask (default) - 尽管您声称“我的通知浏览器设置在此站点上始终允许。
  • 同样的事情发生在我身上,默认情况下,如果网站不安全,chrome 会阻止所有通知
  • 是的 @HimanshuBansal 如果网站不安全,那么它总是拒绝。我在 https 中进行了测试,然后它运行良好。

标签: javascript angularjs notifications


【解决方案1】:

这很简单: [Deprecation] 不能再从不安全的来源使用通知 API。您应该考虑将应用程序切换到安全源,例如HTTPS。有关详细信息,请参阅google's advice。 (匿名)@ ?message=unique-identifier=123:25 ?message=unique-identifier=123:26 被拒绝 你的 lint 应该看起来像这样: [linkExample1][2] - 用于 index.html 或 [linkExampe2][2]


Here is the link 它不能在线工作 但你可以在本地服务器上运行它只需创建任何 html(htm) 文件并在 @987654329 中运行它@protocol 然后在你的 URL 中选择 Allow 选项:

一个小gotcha不要在这个实验室中使用private隐身模式) - 对于 安全原因 不支持推送通知私人隐身模式

    let dnperm = document.getElementById('dnperm');
    let dntrigger = document.getElementById('dntrigger');

    dnperm.addEventListener('click', function(e){
        e.preventDefault();

        if(!window.Notification){
            alert("Notification not supported!");
        }else{
            Notification.requestPermission().then(function(permission) {
                console.log(permission);
                if(permission === 'denied'){
                    alert('You Have Denied Notification!');
                }else if(permission === 'granted'){
                    alert('You Have Granted notification.');
                }
            })
        }
    });

    // simulate

    dntrigger.addEventListener('click', function(e){
        let notify;

        e.preventDefault();

        console.log(Notification.permission);

        if(Notification.permission === 'default'){
            alert('Please allow notification before doing this');
        }else {
            notify = new Notification('New Message From Romzik', {
                body: 'How are you today? Is it really is a lovely day.',
                icon: 'img/msg-icon.png',
                tag: 'unique-identifier=123' // msg-id
            });

            notify.onclick = function (ev) {
                console.log(this);
                window.location = '?message=' + this.tag;
            }
        }


    })
<body>
<a href="" id="dnperm">Request permission</a>
<a href="" id="dntrigger">Trigger</a>
</body>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-17
    • 2023-03-16
    • 2016-01-19
    • 1970-01-01
    • 2015-02-25
    • 2018-07-25
    • 2015-08-30
    相关资源
    最近更新 更多