Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to listen for web notification permission change

According to the Notification.permission spec from MDN, we can check the current user permission for web notification.

However, is there any way to listen for this permission change? Something like this?

.on(Notification.permission, 'changed', function(){ }
like image 266
Li Jing Avatar asked Feb 05 '16 06:02

Li Jing


People also ask

When should you ask for notification permission?

Generally speaking, you should ask users for permissions only when absolutely necessary and only after ensuring that users understand how granting this access will benefit them.


1 Answers

I believe it's a little late for an answer, but... You can use this.

var Notification = window.Notification || window.mozNotification || window.webkitNotification;

var was_questioned = false;
if (Notification.permission == 'default') {
    was_questioned = true;
}

Notification.requestPermission(function (permission) {
    if (was_questioned) {
        console.log("User was asked. New permission is: " + permission);
    }
    if ('permissions' in navigator) {
    navigator.permissions.query({name:'notifications'}).then(function(notificationPerm) {
        notificationPerm.onchange = function() {
            console.log("User decided to change his seettings. New permission: " + notificationPerm.state);
        };
    });
    }
});
like image 166
suz Avatar answered Sep 19 '22 16:09

suz