13

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?

Hans Westman
  • 839
  • 1
  • 15
  • 28

2 Answers2

20

Looking at the documentation on Notification at MDN and WHATWG, there does not seem to be a way to request revocation of permission. However, you could emulate your own version of the permission using localStorage to support that missing functionality. Say you have a checkbox that toggles notifications.

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

You can store your remembered permissions under the notification-permission key in local storage, and update the permission state similar to:

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

You could retrieve the permission as:

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

When you want to display a notification, check your permission:

if (getNotificationPermission() === 'granted') {
    new Notification(/*...*/);
}
Tashows
  • 545
  • 6
  • 21
Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
9

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.

apsillers
  • 112,806
  • 17
  • 235
  • 239
  • Alright, I thought that was the case. Then we'll go for the option of saving it elsewhere Thank you for your answer. – Hans Westman Feb 12 '15 at 13:24