最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Notification permission gives denied always - Stack Overflow

programmeradmin0浏览0评论

I'm using Notification.permission to check whether the browser allows notifications or not. Here is how I check for Notification.permission in my code:

// 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 on my localhost, but when I try to use it in production, it always returns a 'denied' status. My browser settings for notifications are set to 'always allow on this site'. I'm currently unable to identify the issue.

I'm using Notification.permission to check whether the browser allows notifications or not. Here is how I check for Notification.permission in my code:

// 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 on my localhost, but when I try to use it in production, it always returns a 'denied' status. My browser settings for notifications are set to 'always allow on this site'. I'm currently unable to identify the issue.

Share Improve this question edited Feb 4, 2024 at 11:41 Utmost Creator 1,1211 gold badge13 silver badges26 bronze badges asked Nov 23, 2017 at 4:37 DixitDixit 1,3793 gold badges18 silver badges40 bronze badges 6
  • it will always give denied status - your code treats "denied" and "default" as the same thing, are you sure you're denied? – Jaromanda X Commented Nov 23, 2017 at 4:44
  • yes, if prm == 'default' || prm == 'denied' i want to print console.log("permission denied or default"); but prm gives always denied status in production but perfectly works in localhost. – Dixit Commented Nov 23, 2017 at 4:46
  • your notification settings is Ask (default) according to the image - though you claim "my browser settings for notification is always allows on this site." – Jaromanda X Commented Nov 23, 2017 at 4:47
  • 3 the same thing happened to me, By default chrome blocks all notifications if site is not secure – Himanshu Bansal Commented Nov 30, 2017 at 12:36
  • 1 yes @HimanshuBansal if the site is not secure then it always gives denied. I tested in https then it works perfectly. – Dixit Commented Dec 1, 2017 at 3:39
 |  Show 1 more comment

1 Answer 1

Reset to default 13

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>

发布评论

评论列表(0)

  1. 暂无评论