Skip to content

Instantly share code, notes, and snippets.

@JacobBennett
Last active January 25, 2016 04:28
Show Gist options
  • Save JacobBennett/5c8906ab1c7eed255896 to your computer and use it in GitHub Desktop.
Save JacobBennett/5c8906ab1c7eed255896 to your computer and use it in GitHub Desktop.
Web Desktop Notifications Module

Web Desktop Notfications

Include the FormNotifyModule.js in your page. Once your page is loaded, call FormNotify.send('message here') to send a desktop notification to your user.

Click Here to see an example

var DesktopNotify = (function(){
var bindUIElements = function() {
window.addEventListener('load', getPermission);
};
var init = function() {
bindUIElements();
};
var getPermission = function () {
if (window.Notification && Notification.permission !== "granted") {
Notification.requestPermission(function (status) {
if (Notification.permission !== status) {
Notification.permission = status;
}
});
}
};
var send = function (message, tag) {
if (typeof(tag) == "undefined") {
tag = "default-notification";
}
if (window.Notification && Notification.permission === "granted") {
new Notification(message, {tag: tag});
}
// If the user hasn't told if he wants to be notified or not
// Note: because of Chrome, we are not sure the permission property
// is set, therefore it's unsafe to check for the "default" value.
else if (window.Notification && Notification.permission !== "denied") {
Notification.requestPermission(function (status) {
if (Notification.permission !== status) {
Notification.permission = status;
}
if (status === "granted") {
new Notification(message, {tag: tag});
}
});
}
};
return {
init: init,
send: send
}
})();
DesktopNotify.init();
<html>
<body>
<button>Click Me</button>
<script src="DesktopNotifyModule.js" type="text/javascript"></script>
<script>
var button = document.getElementsByTagName('button')[0];
button.addEventListener('click', function () {
DesktopNotify.send('Example Message Here');
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment