javascript - 通知权限总是被拒绝

标签 javascript angularjs notifications

我正在使用 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 中运行良好,但当我尝试在生产中使用时,它总是会给出拒绝状态。 我的浏览器通知设置是在此站点上始终允许enter image description here 但我不知道是什么问题。 需要帮助。

最佳答案

它说的很简单: [Deprecation] 可能不再从不安全的来源使用 Notification 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) 文件并HTTPS 中运行它协议(protocol),然后在您的 URL 中选择 Allow 选项: Hear is what i mean

One small gotcha do not use private(incognito mode) for this lab - for security reason push notifications are not supported in private or incognito mode

    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>

关于javascript - 通知权限总是被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47447637/

相关文章:

javascript - 你能在 Less 中部分匹配类吗?

javascript - 嵌套 Async ForEach 循环

javascript - 具有动态选择字段的表单无法验证

android - 在 Android 中通过通知栏复制粘贴

java - 应用程序图标上的新通知计数弹出窗口

javascript - 如何获取 JavaScript 对象的大小?

Javascript 追加和撤消问题

javascript - Angular : "TypeError: Cannot read property ' insertBefore' of null"while using Slick Slider

javascript - 使用 AngularJS 在 Apache Zeppelin 中绘制多条线(折线图)

ios5 - iOS如何检测电源线是否插入