通知权限始终被拒绝

Dix*_*xit 5 javascript notifications angularjs

我在用 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");
    }
Run Code Online (Sandbox Code Playgroud)

这段代码在我中运行正常,localhost但是当我尝试在生产环境中使用时,它始终会处于拒绝状态。我的浏览器通知设置始终在此站点上允许在此处输入图片说明 但我不知道是什么问题。需要帮助。

Utm*_*tor 9

这很容易说: [Deprecation]通知 API 可能不再从不安全的来源使用。您应该考虑将应用程序切换到安全来源,例如HTTPS. 有关更多详细信息,请参阅谷歌的建议。(匿名)@ ?message=unique-identifier=123:25 ?message=unique-identifier=123:26 否认你的 lint 应该看起来像这样: [linkExample1][2]- 对于 index.html 或[linkExampe2][2]


这是 它无法在线 运行的链接但您可以在本地服务器上运行它,只需创建任何 html(htm) 文件并HTTPS协议中运行它,然后Allow在您的 URL 中选择选项: 听到是我的意思

一个小问题不要在本实验中使用私密隐身模式)——出于 安全原因私密隐身模式不支持推送通知

    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;
            }
        }


    })
Run Code Online (Sandbox Code Playgroud)
<body>
<a href="" id="dnperm">Request permission</a>
<a href="" id="dntrigger">Trigger</a>
</body>
Run Code Online (Sandbox Code Playgroud)