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(){ }

Answers:

Answer

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);
        };
    });
    }
});
Answer

You just neeed

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);
    };
  });
}

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.