如何侦听网络通知权限更改 [英] How to listen for web notification permission change

查看:34
本文介绍了如何侦听网络通知权限更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Notification.permission 规范来自MDN,我们可以查看当前用户的网页通知权限.

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

推荐答案

我认为现在给出答案有点晚了,但是...你可以使用这个.

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

这篇关于如何侦听网络通知权限更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆