Created
May 8, 2015 03:07
-
-
Save wang-steven/8e1bc9f39f50df6ea110 to your computer and use it in GitHub Desktop.
HTML5 web notification on desktop
This file contains hidden or 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
<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