Created
March 14, 2016 17:50
-
-
Save freekrai/55240c42faa773e02b1b to your computer and use it in GitHub Desktop.
push notifications in browser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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