javascript - Notification.permission in chrome always return denied - Stack Overflow

I have a website that asks notification permission from user during log-in. I have a code there that au

I have a website that asks notification permission from user during log-in. I have a code there that auto-logouts the user when he/she denies the permission (see the codes below)

//Checks if user browsers notification is "Allowed", if "denied", logs out user
if(Notification.permission == 'denied') {
     removeAllCookies($cookies);
     $location.path("/login");
}

That code seems ok at first but just today it started malfunctioning. All new users auto-logouts even if they haven't configured the permission yet.

I even tried to check the permission manually at the favicon at the left of address bar and I can see that it is still set to ask(default). I also tried to manually set it to Allow yet the Notification.permission function still returns denied. What's causing this? It is ok in my pc but in all other users it auto-blocks.

EDIT: Just now I'm already experiencing the same thing on my PC. My Notification permission is Allowed but Notification.permission always return denied. I console logged it and it really does return denied. Anyone?

I have a website that asks notification permission from user during log-in. I have a code there that auto-logouts the user when he/she denies the permission (see the codes below)

//Checks if user browsers notification is "Allowed", if "denied", logs out user
if(Notification.permission == 'denied') {
     removeAllCookies($cookies);
     $location.path("/login");
}

That code seems ok at first but just today it started malfunctioning. All new users auto-logouts even if they haven't configured the permission yet.

I even tried to check the permission manually at the favicon at the left of address bar and I can see that it is still set to ask(default). I also tried to manually set it to Allow yet the Notification.permission function still returns denied. What's causing this? It is ok in my pc but in all other users it auto-blocks.

EDIT: Just now I'm already experiencing the same thing on my PC. My Notification permission is Allowed but Notification.permission always return denied. I console logged it and it really does return denied. Anyone?

Share Improve this question edited Dec 4, 2017 at 3:45 asked Nov 17, 2017 at 2:01 user8602307user8602307 2
  • Do you mean it works on localhost but not on a different site? – Josh Lee Commented Nov 17, 2017 at 2:41
  • no sir. What I mean is it works in my pc but auto logouts on other pc(s) that are accessing the website – user8602307 Commented Nov 17, 2017 at 3:01
Add a ment  | 

4 Answers 4

Reset to default 2

Strange business logic you've got there, but for what it's worth...

There are a few stipulations Chrome puts in place:

1) Must be using HTTPS or localhost. If you are not, it will automatically deny the permissions in my experience

2) Works more reliably if you register a service worker (I think it's actually required for mobile chrome)

Make sure to read documentation surrounding Notifications API thoroughly, as you'll learn some stuff. MDN is a great resource.

Something else that might help is ensuring you have your notification logic wrapped in the requestPermission promise:

Notification.requestPermission().then(function(permission) {
//if permission == 'denied' // logout
}

Let me know if any of this helps.

are you loading the permission inside an iframe? Seems like a chrome change, where Notification.permission will return denied if asked within an iframe

If you are using in CodePen or JSFiddle, where code is running inside an iframe, the permission will always return denied on Windows and Linux. On macOS works fine.

  • https://codepen.io/anon/pen/QomMVe
  • https://jsfiddle/7qwdy4xb/

The ability to call Notification.requestPermission() from non-main frames had been removed from Chrome since version 60, but still works fine on Firefox at time of writing.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742309203a4419548.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信