Last active
March 7, 2018 12:27
-
-
Save hardiksondagar/450d804a0b8e5a313d52 to your computer and use it in GitHub Desktop.
Chrome desktop Notification example
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
<button onclick="notifyMe()">Notify me!</button> |
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
// Credit : http://stackoverflow.com/questions/2271156/chrome-desktop-notification-example | |
// request permission on page load | |
document.addEventListener('DOMContentLoaded', function () { | |
if (Notification.permission !== "granted") | |
Notification.requestPermission(); | |
}); | |
function notifyMe() { | |
if (!Notification) { | |
alert('Desktop notifications not available in your browser. Try Chromium.'); | |
return; | |
} | |
if (Notification.permission !== "granted") | |
Notification.requestPermission(); | |
else { | |
var notification = new Notification('Notification title', { | |
icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png', | |
body: "Hey there! You've been notified!", | |
}); | |
notification.onclick = function () { | |
window.open("http://stackoverflow.com/a/13328397/1269037"); | |
}; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment