Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wang-steven/8e1bc9f39f50df6ea110 to your computer and use it in GitHub Desktop.
Save wang-steven/8e1bc9f39f50df6ea110 to your computer and use it in GitHub Desktop.
HTML5 web notification on desktop
<html>
<head></head>
<body>
<button>Show Desktop Notification</button>
<script type="text/javascript">
var btn = document.querySelector('button');
var notifications = window.Notification;
if (notifications) {
btn.addEventListener('click', showMsg, false);
} else {
console.log('Desktop Notification does not support.');
}
function showMsg() {
if (notifications.permission === 'granted') {
var demo = new Notification('Title', {
body: 'Hello'
});
demo.onshow = function() {
console.log('The notification is showed');
setTimeout(function() {
demo.close();
}, 10000);
};
demo.onclick = function() {
console.log('You click the notification');
window.focus();
};
demo.onclose = function() {};
demo.onerror = function() {};
} else {
notifications.requestPermission();
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment