Skip to content

Instantly share code, notes, and snippets.

@JessieAMorris
Created October 22, 2013 00:48
Show Gist options
  • Save JessieAMorris/7093460 to your computer and use it in GitHub Desktop.
Save JessieAMorris/7093460 to your computer and use it in GitHub Desktop.
HTML5 Notifications demo
<html>
<head>
<style type="text/css">
.hide {
display: none;
}
</style>
</head>
<body>
<div class="create-notification-pane hide">
<button id="createNotification">Create A Basic Notification</button>
</div>
<div class="not-supported-pane">
Sorry, but your browser isn't supported. You should upgrade to either Firefox or Chrome.
</div>
<script type="text/javascript">
if(typeof window.Notification !== 'undefined') {
document.querySelector(".not-supported-pane").classList.add("hide");
document.querySelector(".create-notification-pane").classList.remove("hide");
}
function createNotification(){
if(Notification) {
if(Notification.permission === "granted") {
var notification = new Notification("Hello, world!");
} else if(Notification.permission !== "denied") { // We only want to do this if the user hasn't explicitly said no
Notification.requestPermission(function(permission) {
// Chrome doesn't store this, so let's do that for them.
if(!Notification.permission || Notification.permission !== permission) {
Notification.permission = permission;
}
if(Notification.permission === "granted") {
createNotification();
}
});
}
} else {
document.querySelector(".not-supported-pane").classList.remove("hide");
}
}
var button = document.querySelector("#createNotification");
button.addEventListener("click", createNotification);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment