通知权限总是被拒绝 [英] Notification permission gives denied always

查看:500
本文介绍了通知权限总是被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  //让我们检查浏览器是否支持通知
if(!(Notification)在窗口中)){
alert(此浏览器不支持桌面通知);
}
var prm = Notification.permission;
if(prm =='default'|| prm =='denied'){
console.log(permission denied or default);
} else {
console.log(允许许可);
}

此代码在我的 localhost中工作正常但是当我尝试在生产中使用时,它总是会被拒绝状态。
我的浏览器通知设置始终允许在此网站上

但我不知道问题是什么。需要
帮助。

解决方案

这很容易说:
[弃用] Notification API可能不再用于不安全的来源。您应该考虑将应用程序切换到安全的来源,例如 HTTPS 。请参阅


一个小的陷阱不使用私人隐身此实验室的模式) - 在私人
中支付
安全原因 不支持推送通知隐身模式


  let dnperm = document.getElementById('dnperm');让dntrigger = document.getElementById('dntrigger'); dnperm.addEventListener('click',function(e){e.preventDefault(); if(!window.Notification){alert(Notification not supported!);} else {Notification.requestPermission()。then(function(权限){console.log(权限); if(权限==='拒绝'){alert('你已经拒绝通知!');}否则if(permission ==='grant'){alert('你有'授予通知。');}})}}); //模拟dntrigger.addEventListener('click',function(e){let notify; e.preventDefault(); console.log(Notification.permission); if(Notification.permission ==='default'){alert('请在执行此操作前允许通知');} else {notify = new Notification('来自Romzik的新消息',{body:'你今天好吗?这真的是一个美好的一天。',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>请求权限< / a>< a href =id =dntrigger>触发器< / a>< / body>  


I am using Notification.permission for check whether the browser allows notification or not.

my code for check Notification permission like below.

    // 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");
    }

This code is working fine in my localhost but when I try to use in production it will always give denied status. my browser settings for notification is always allows on this site. but I do not figure out what is the problem. help needed.

解决方案

That's easy it says: [Deprecation] The Notification API may no longer be used from insecure origins. You should consider switching your application to a secure origin, such as HTTPS. See google's advice for more details. (anonymous) @ ?message=unique-identifier=123:25 ?message=unique-identifier=123:26 denied you lint should look smth like this: [linkExample1][2] - for index.html or [linkExampe2][2]


Here is the link it will not work online but you can run it on local server just create any html(htm) file and run it in HTTPS protocol then select Allow option in your URL:

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>

这篇关于通知权限总是被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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