Skip to content

Instantly share code, notes, and snippets.

@TerryMooreII
Created January 27, 2014 15:25
Show Gist options
  • Save TerryMooreII/8650419 to your computer and use it in GitHub Desktop.
Save TerryMooreII/8650419 to your computer and use it in GitHub Desktop.
HTML5 Notifications examaple
<html>
<head>
<title>Teset</title>
</head>
<body>
<button onclick="notifyMe()">Notify me!</button>
<script type="text/javascript">
function notifyMe() {
var obj = {
body:"Stairway To Heaven by Led Zeppelin",
icon: "http://superhypeblog.com/wp-content/uploads/2011/08/led-zep-iv.jpg"
}
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}
// Let's check if the user is okay to get some notification
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
var notification = new Notification("Now Playing...", obj);
}
// Otherwise, we need to ask the user for permission
// Note, Chrome does not implement the permission static property
// So we have to check for NOT 'denied' instead of 'default'
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
// Whatever the user answers, we make sure Chrome stores the information
if(!('permission' in Notification)) {
Notification.permission = permission;
}
// If the user is okay, let's create a notification
if (permission === "granted") {
var notification = new Notification("Hi there!");
}
});
}
setTimeout(function(){
notification.close()
} , 5000);
// At last, if the user already denied any notification, and you
// want to be respectful there is no need to bother him any more.
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment