Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification permission gives denied always

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. enter image description here but I do not figure out what is the problem. help needed.

like image 267
Dixit Avatar asked Nov 23 '17 04:11

Dixit


People also ask

How do you reset notification permissions?

If you do not have a notification open, open Chrome on Android, tap the 3-dot menu, Settings, Site settings (under Advanced), Notifications, make sure it's set to "Ask before sending (recommended)". Find your site on the list, click the entry, and click Clear and Reset.

When should you ask for notification permission?

Generally speaking, you should ask users for permissions only when absolutely necessary and only after ensuring that users understand how granting this access will benefit them.


1 Answers

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: 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>
like image 81
Utmost Creator Avatar answered Oct 01 '22 01:10

Utmost Creator