通知权限总是被拒绝

问题描述 投票:0回答:1

我正在使用

Notification.permission
检查浏览器是否允许通知。 以下是我在代码中检查
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
上运行良好,但是当我尝试在生产中使用它时,它总是返回“denied”状态。我的浏览器通知设置设置为“始终允许此网站”。 我目前无法确定问题所在。

javascript angularjs notifications
1个回答
12
投票

这很简单:

[Deprecation]
通知 API 可能不再从不安全的来源使用。您应该考虑将应用程序切换到安全来源,例如
HTTPS
。请参阅 google 的建议 了解更多详情。 (匿名)@?message=unique-identifier=123:25 ?message=unique-identifier=123:26 被拒绝 你的 lint 应该看起来像这样:
[linkExample1][2]
- 用于index.html 或
[linkExampe2][2]


这是链接它无法在线工作但您可以在本地服务器上运行它只需创建任何html(htm)文件并

HTTPS
协议中运行它然后在您的URL中选择
Allow
选项:

一个小问题问题请勿在本实验中使用私人隐身模式) - 用于 安全原因不支持推送通知私人隐身模式

    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>

© www.soinside.com 2019 - 2024. All rights reserved.