Skip to content

Instantly share code, notes, and snippets.

@aorcsik
Created September 23, 2012 08:12
Show Gist options
  • Save aorcsik/3769318 to your computer and use it in GitHub Desktop.
Save aorcsik/3769318 to your computer and use it in GitHub Desktop.
HTML5 Notifications
document.write('<input id="shownoti" style="display: none;" type="button" value="Show Notification" />');
document.write('<div style="display: none;" id="notidenied">Az értesítések megjelenítését letiltottad!</div>');
var displayNotiButton = function() {
if (window.webkitNotifications.checkPermission() == 0) {
document.getElementById("shownoti").onclick = function() {
var notification = webkitNotifications.createNotification("icon.png", "blog.intiweb.hu", "Értesítve vagy!");
notification.addEventListener('display', function() {
window.setTimeout(function() {
notification.cancel();
}, 5000);
});
notification.show();
};
document.getElementById("shownoti").style.display = "inline";
return true;
} else if (window.webkitNotifications.checkPermission() == 2) {
document.getElementById("notidenied").style.display = "block";
return true;
} else if (window.webkitNotifications.checkPermission() == 1) {
return false;
}
};
if (window.webkitNotifications) if (false === displayNotiButton()) {
document.write('<input type="button" value="Request Notification Permission" id="reqperm">');
document.getElementById("reqperm").addEventListener('click', function() {
window.webkitNotifications.requestPermission(function() {
document.getElementById("reqperm").style.display = "none";
displayNotiButton();
});
});
}
if (window.webkitNotifications.checkPermission() == 0) {
/* API kód */
} else if (window.webkitNotifications.checkPermission() == 1) {
document.write('<input id="reqperm" type="button" value="Request Notification Permission" />');
document.getElementById("reqperm").addEventListener('click', function() {
window.webkitNotifications.requestPermission(function() {
/* API kód */
});
});
}
var notification = webkitNotifications.createNotification("icon.png", "blog.intiweb.hu", "Értesítve vagy!");
notification.addEventListener('display', function() {
window.setTimeout(function() {
notification.cancel();
}, 5000);
});
notification.show();
if (window.webkitNotifications) { /* ... */ }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment