如何监听Web通知权限更改

时间:2016-02-05 06:50:52

标签: javascript web permissions notifications listener

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

但是,有没有办法听取此权限更改?像这样的东西?

.on(Notification.permission, 'changed', function(){ }

2 个答案:

答案 0 :(得分:4)

我相信答案有点迟,但是......你可以使用它。

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

答案 1 :(得分:0)

您刚刚笑了

private void Button1_Click(object sender, EventArgs e)
{
    int counter = 0;
    string line;
    System.IO.StreamReader file = new System.IO.StreamReader("Sav1.txt");
    while ((line = file.ReadLine()) != null)
    {
        richTextBox1.Items.Add(line);
        counter++;
    }
}