denying browser notification permissions

不问归期 提交于 2021-01-27 12:49:44

问题


According to the spec, you can prompt the browser to allow the user to grant or deny browser notifications. Once the user has granted permission, is there a way to programmatically deny that permission too?

Calling window.Notification.requestPermission() after they have granted permission does nothing except run the success promise.

My goal is to have an On/Off button toggle in the user settings portion of my site. As far as i can tell, there is no way to toggle off once toggled on.


回答1:


Unfortunately I don't think this is possible at the moment.

In the future we should hopefully be able to use the Permissions API to check and revoke notifications (as well as other APIs like geolocation etc).

However, while we can currently query the status of a permission, the revoke method is unsupported. I believe it was possible in Firefox until recently, but in 51 the functionality was changed to default to off. As per MDN:

The revoke() function has been disabled by default starting in Firefox 51, since its design has been brought into question in the Web Applications Security Working Group. It can be re-enabled by setting the preference dom.permissions.revoke.enable to true.

Example of permissions API:

navigator.permissions.query({name:'notifications'}).then(function(result) {

  console.log(result);

});

Result is prompt/denied/granted. Change 'query' to 'revoke' for denying permissions.

navigator.permissions.revoke({name:'notifications'});

Alternatively, this answer from a couple of years ago suggests an alternate approach which you might find interesting - using localStorage to approximate this functionality.



来源:https://stackoverflow.com/questions/43419116/denying-browser-notification-permissions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!