Web Notifications

by Joshua McNeese

HTML

<button id="button">Create notification</button>

JavaScript

//Compatibility (Chrome Only)
window.Notifications = window.webkitNotifications;

document.getElementById("button").addEventListener("click", function() {
    
    if (window.Notifications.checkPermission() == 0) { // Allowed
        var icon = "http://cdn1.iconfinder.com/data/icons/iconbeast-lite/30/bomb.png",
            title = "Web Notification",
            message = "Praesent commodo cursus magna, vel scelerisque nisl consectetur et.";
        
        var notification = window.Notifications.createNotification(icon, title, message);
        
        //Handlers
        notification.onshow = function() {
            console.log("onshow fired");
            setTimeout(function() {
                notification.close();
            }, 5000);
        };
        
        notification.onclick = function() {
            console.log("onclick fired");
        };
        
        notification.onerror = function() {
            console.log("onerror fired");
        };
        
        notification.onclose = function() {
            console.log("onclose fired");
        };
        
        notification.show();
        
    }
    else {
        
        window.Notifications.requestPermission();
    }
});