网络通知无效

时间:2013-10-17 15:28:47

标签: html5 html5-notifications

我想在这个例子中使用网络通知API: http://www.inserthtml.com/2013/10/notification-api/?utm_source=html5weekly&utm_medium=email

当我在这个网站上时,一切都运行良好,在控制台中我正在写“Notification.permission”并获得“授权”。

但是,如果我想在我的网站上做同样的事情,我收到有关Notification对象的错误,当我正在尝试打印“Notification.permission”时,我注意到Notification对象没有拥有此属性和其他属性,如“requestPermition”。

在所有浏览器中都会发生这种情况,并且它们都已更新到最新版本。

我试图在其他网站上打开控制台,例如cnn.com,并检查Notifications对象,还有缺少的属性。

知道为什么??以及它如何在上面的网站上工作? 感谢。

这是我的代码:

window.addEventListener('load',function(){     var button = document.getElementById(“notifications”);

function theNotification() {

    var n = new Notification("Hi!",  {
    });

}

// When the button is clicked
button.addEventListener('click', function () {

    // If they are not denied (i.e. default)
    if (Notification && Notification.permission !== "denied") {

        // Request permission
        Notification.requestPermission( function( status ){

            // Change based on user's decision
            if (Notification.permission !== status)
                Notification.permission = status;
        });
    }
});

$(button).click();

var socket = io.connect('http://localhost:3000', {query : "user=343"});
socket.on('notification', function (data) {

    console.log( data );

    if (Notification && Notification.permission === "granted") {
        theNotification();

    } else {
        alert(data);
    }
});

});

0 个答案:

没有答案