Search code examples
notificationschatlivechat

I have created a chat application in which every ten second it takes records from database but i want to show notification at taskbar


I want to show new message notification at taskbar in asp.net MVC or in somewhere to aware user that new message have came.


Solution

  • We can show desktop notification by javascript function

    function createNotification() {

        var options = {
            body: 'This is the body of the notification',
            icon: 'stupidcodes.com.png',
            dir: 'ltr'
        };
    
        var notification = new Notification("Hi there", options);
    
        notification.onclick = function () {
            window.open(document.URL);
        };
    }
    
    function notifyMe() {
        if (!("Notification" in window)) {
            alert("This browser does not support desktop notification");
        }
        else if (Notification.permission === "granted") {
    
            createNotification();
    
        }
        else if (Notification.permission !== 'denied') {
            Notification.requestPermission(function (permission) {
                if (!('permission' in Notification)) {
                    Notification.permission = permission;
                }
                if (permission === 'granted') {
    
                    createNotification();
                }
            });
        }
    }
    

    first check throgh ajax function if there is any unread funtion then call this notifyMe() function