Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

javascript - 如何收听Web通知权限更改(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.

(根据MDN的Notification.permission规范,我们可以检查当前用户的Web通知权限。)

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

(但是,有什么方法可以监听此权限更改?)

Something like this?

(像这样吗)

.on(Notification.permission, 'changed', function(){ }
  ask by Li Jing translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...