Remove HTML5 notification permissions

和自甴很熟 提交于 2019-12-05 02:01:05

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

If you don't want to show notifications, simply don't call new Notification.

You can either wrap all your calls to new Notification inside conditions:

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

Or you can rewrite the Notification constructor to contain a contiditional and call the original Notification as appropriate:

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

If you use vendor-prefixed constructors or functions (e.g., webkitNotifications.createNotification), then you'll need to rewrite each of those as well to be conditional on your options variable.

Looking at the documentation on Notification at MDN and WHATWG, there does not seem to be a way to request revocation of permissions. However, you could emulate your own version of the permissions using localStorage to support that missing functionality. 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:

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(/*...*/);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!