Notification API

by stopsatgreen

HTML

<button disabled>Alert</button>

JavaScript

var button = document.querySelector('button');
window.Notifications = window.Notifications || window.webkitNotifications;
function hasPermish() {
    if (window.Notifications.checkPermission() == 0) {
        // Allowed
        button.removeAttribute('disabled');    
    } else {
        // request a user permission
        window.Notifications.requestPermission();
    }        
}

if(window.Notifications) {
        var notification = window.Notifications.createNotification(            'http://cdn1.iconfinder.com/data/icons/simplicio/32x32/notification_warning.png',
        'Here is the news',
        'Nothing untowards has happened'
    );
    //var notification = window.Notifications.createHTMLNotification('foo.html');
    hasPermish();
} else {
    window.alert('No notifications');       
}

document.querySelector('button').addEventListener('click', function(e){
    e.preventDefault;
    notification.show();
}, false);