拒绝浏览器通知权限 [英] denying browser notification permissions

查看:34
本文介绍了拒绝浏览器通知权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据规范,您可以提示浏览器以允许用户授予或拒绝浏览器通知.一旦用户授予了权限,是否还有办法以编程方式拒绝该权限?

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?

在授予权限后调用 window.Notification.requestPermission()不会执行任何操作,除非运行成功承诺.

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.

推荐答案

不幸的是,我目前不认为这是可能的.

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

将来,我们应该希望能够使用 权限API ,用于检查和撤消通知(以及地理位置等其他API).

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).

但是,尽管我们目前可以查询权限状态,但不支持吊销方法.我相信直到最近,Firefox才有可能,但是在51中,该功能已更改为默认设置为关闭".根据 MDN :

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:

从Firefox开始,默认情况下已禁用revoke()函数51,因为其设计已在网络上受到质疑应用程序安全工作组.可以通过设置重新启用偏好dom.permissions.revoke.enable为true.

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.

权限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'});


或者,几年前此答案提出了一种替代方法,您可以觉得很有趣-使用localStorage近似此功能.


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

这篇关于拒绝浏览器通知权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆