Showing posts with label Desktop Notification. Show all posts
Showing posts with label Desktop Notification. Show all posts

Saturday, December 17, 2016

Adding Desktop Notifications to Your Web Applications

Download example from here


<button onclick="notifyMe()">Notify me!</button>

<script type="text/javascript">
    // request permission on page load
    document.addEventListener('DOMContentLoaded', function () {
        if (!Notification) {
            alert('Desktop notifications not available in your browser.');
            return;
        }
        if (Notification.permission !== "granted") {
            Notification.requestPermission();
        }
    });

    function notifyMe() {
        if (Notification.permission !== "granted") {
            Notification.requestPermission();
        }
        else {
            setTimeout(function() {
                var notification = new Notification('Notification Title', {
                    icon: 'images.jpg',
                    body: "Hey there! You've been notified!, Hey there! You've been notified!, Hey there! You've been notified!",
                });

                notification.onclick = function () {
                    alert("Notification event clicked");
                };
            }, 1000);
        }
    }
</script>