Removing HTML5 Notification Permissions

You can offer the user to enable or disable desktop alerts from the browser by doing:

Notification.requestPermission(callback); 

But is it possible to remove this permission using code? We want our users to have the option to switch notifications. Can this be achieved using JavaScript, or do we need to save this parameter elsewhere?

+6
source share
2 answers

No, your script does not have the ability to programmatically refuse permission to display notifications. The API specification has no rights functions other than requestPermission . (Of course, the browser may have an options menu that allows the user to revoke the permission for the domain, but the option is at the browser level, not the site level option. For example, in Chrome you can see this option menu by clicking the icon to the left of the address bar.)

If you do not want to show notifications, just do not call new Notification .

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

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

Or you can rewrite the Notification constructor to contain contiditional and invoke the original Notification depending on the situation:

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

If you use the constructors or functions created by the vendor prefix (for example, webkitNotifications.createNotification ), you will also need to rewrite each of them to be dependent on the parameter variable.

+7
source

See the documentation for Notification in MDN and WHATWG , there seems to be no way to request permission revocation. However, you can emulate your own version of permissions with localStorage to support this missing functionality. Say you have a checkbox that toggles notifications.

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

You can save your remembered permissions under the notification-permissions key in local storage and update the permission status like this:

 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 can get permissions like:

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

If you want to display a notification, check your rights:

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

Source: https://habr.com/ru/post/982383/


All Articles