简体   繁体   English

删除HTML5通知权限

[英]Remove HTML5 notification permissions

You can prompt a user to allow or deny desktop notifications from the browser by running: 您可以通过运行以下命令提示用户允许或拒绝来自浏览器的桌面通知:

Notification.requestPermission(callback);

But is it possible to remove that permission by code? 但是可以通过代码删除该权限吗? We want our users to have the option to toggle notifications. 我们希望用户可以选择切换通知。 Can this be achieved by JavaScript or do we need to save that option elsewhere? 这可以通过JavaScript实现,还是我们需要在其他地方保存该选项?

Looking at the documentation on Notification at MDN and WHATWG , there does not seem to be a way to request revocation of permissions. 查看MDNWHATWG上的Notification文档,似乎没有办法请求撤销权限。 However, you could emulate your own version of the permissions using localStorage to support that missing functionality. 但是,您可以使用localStorage模拟自己的权限版本,以支持缺少的功能。 Say you have a checkbox that toggles notifications. 假设您有一个切换通知的复选框。

<input type="checkbox" onChange="toggleNotificationPermissions(this);" />

You can store your remembered permissions under the notification-permissions key in local storage, and update the permission state similar to: 您可以将记住的权限存储在本地存储中的notification-permissions键下,并更新权限状态,类似于:

function toggleNotificationPermissions(input) {
    if (Notification.permissions === 'granted') {
        localStorage.setItem('notification-permissions', input.checked ? 'granted' : 'denied');
    } else if (Notification.permissions === 'denied') {
        localStorage.setItem('notification-permissions', 'denied');
        input.checked = false;
    } else if (Notification.permissions === 'default') {
        Notification.requestPermission(function(choice) {
            if (choice === 'granted') {
                localStorage.setItem('notification-permissions', input.checked ? 'granted' : 'denied');
            } else {
                localStorage.setItem('notification-permissions', 'denied');
                input.checked = false;
            }
        });
    }
}

You could retrieve the permissions as: 您可以将权限检索为:

function getNotificationPermissions() {
    if (Notification.permissions === 'granted') {
        return localStorage.getItem('notification-permissions');
    } else {
        return Notification.permissions;
    }
}

When you want to display a notification, check your permissions: 如果要显示通知,请检查您的权限:

if (getNotificationPermissions() === 'granted') {
    new Notification(/*...*/);
}

No, there is no way for your script to programmatically relinquish permission to show notifications. 不,您的脚本无法以编程方式放弃显示通知的权限。 The API specification does not have any permission-related functions aside from requestPermission . 除了requestPermission之外, API规范没有任何与权限相关的功能。 (Of course, a browser may have an options menu that allows the user to revoke permission for a domain, but that's a browser-level option, not a site-level option. For example, in Chrome, you can see this options menu by clicking the icon in the left of the address bar.) (当然,浏览器可能有一个选项菜单,允许用户撤消域的权限,但这是浏览器级选项,而不是网站级选项。例如,在Chrome中,您可以看到此选项菜单单击地址栏左侧的图标。)

If you don't want to show notifications, simply don't call new Notification . 如果您不想显示通知,只需不要调用new Notification

You can either wrap all your calls to new Notification inside conditions: 您可以在条件内将所有调用包装到new Notification

if(notifications_allowed) {
    new Notification(...);
}

Or you can rewrite the Notification constructor to contain a contiditional and call the original Notification as appropriate: 或者,您可以重写Notification构造函数以包含contiditional并根据需要调用原始Notification

(function() {
    var oldNofitication = Notification;
    Notification = function() {
        if(notifications_allowed) {
            oldNotification.apply(this, arguments);
        }
    }
})();

If you use vendor-prefixed constructors or functions (eg, webkitNotifications.createNotification ), then you'll need to rewrite each of those as well to be conditional on your options variable. 如果您使用供应商前缀的构造函数或函数(例如, webkitNotifications.createNotification ),那么您还需要重写每个构造函数或函数,以条件取决于您的options变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM