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.
1 Answer
Reset to default 13That'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 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:44Ask (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