Created
December 23, 2021 22:01
-
-
Save natchiketa/0824f1742ddcd3e67404e351af63e5a3 to your computer and use it in GitHub Desktop.
gOGGEdr
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
<button>Click 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
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