Skip to content

Instantly share code, notes, and snippets.

@natchiketa
Created December 23, 2021 22:01
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 natchiketa/0824f1742ddcd3e67404e351af63e5a3 to your computer and use it in GitHub Desktop.
Save natchiketa/0824f1742ddcd3e67404e351af63e5a3 to your computer and use it in GitHub Desktop.
gOGGEdr
<button>Click Me</button>
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,
getPermission: getPermission
};
})();
document.addEventListener(
"DOMContentLoaded",
function () {
DesktopNotify.init();
DesktopNotify.getPermission();
var button = document.getElementsByTagName("button")[0];
button.addEventListener("click", function () {
DesktopNotify.send("Example Message Here");
});
},
false
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment