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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With