Skip to content

Instantly share code, notes, and snippets.

@freekrai
Created March 14, 2016 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freekrai/55240c42faa773e02b1b to your computer and use it in GitHub Desktop.
Save freekrai/55240c42faa773e02b1b to your computer and use it in GitHub Desktop.
push notifications in browser
<!-- Learn about this code on MDN: https://developer.mozilla.org/en-US/docs/Web/API/notification -->
<script src="notify.js"></script>
<button onclick="notifyMe()">Notify me!</button>
function notifyMe() {
// Let's check if the browser supports notifications
if ( !("Notification" in window) ) {
alert("This browser does not support desktop notification");
}else if (Notification.permission === "granted") {
// Let's check whether notification permissions have already been granted
// If it's okay let's create a notification
var notification = new Notification("Hi there!");
}else if (Notification.permission !== 'denied') {
// Otherwise, we need to ask the user for permission
Notification.requestPermission(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification("Hi there!");
}
});
}
}
Notification.requestPermission();
function spawnNotification(theBody,theIcon,theTitle) {
var options = {
body: theBody,
icon: theIcon
}
var n = new Notification(theTitle,options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment