locked
SignalR push notifications RRS feed

  • Question

  • User-220572584 posted

    Hi 

    I want to show a notification when my

    connection.on("NotificationForRequest", function () {

    gets triggered 

    I actually have tried 

          function showNotification() {
                        debugger;
                        const notification = new Notification("New message incoming", {
                            body: "Hi there. How are you doing?"
                        })
                    }

                    console.log(Notification.permission);
                    if (Notification.permission === "granted") {
                        showNotification();
                    } else if (Notification.permission !== "denied") {
                        Notification.requestPermission().then(permission => {
                            if (permission === "granted") {
                                showNotification();
                            }
                        });
                    }

    but nothing happens 

    I want to show a notification that even if the webpage is minimized on clients server gets poped up and informs him

    any Ideas?

    my whole code is 

    connection.on("NotificationForRequest", function () {

                    debugger;

                    function showNotification() {
                        debugger;
                        const notification = new Notification("New message incoming", {
                            body: "Hi there. How are you doing?"
                        })
                    }

                    console.log(Notification.permission);
                    if (Notification.permission === "granted") {
                        showNotification();
                    } else if (Notification.permission !== "denied") {
                        Notification.requestPermission().then(permission => {
                            if (permission === "granted") {
                                showNotification();
                            }
                        });
                    }

                    debugger;
              
               
                   
                   

                    
                    $('#NotificationModal').modal("show");

                })

    thank you

    Saturday, February 20, 2021 11:59 AM

All replies

  • User1535942433 posted

    Hi kawmy,

    According to your description,As far as I think,each active tab is one connection to SignalR (the Client), with a unique ConnectionId. Depending on the usage of notifications, a visitor does not have to be logged in. A connection with a SignalR Hub is established when the JavaScript code has been initialized.

    You can simply invoke (call) a JavaScript function from the server for each Client (visitor). So all visitors will receive the notification.

    All connected clients will receive this 'event' from the server. Write a 'listener' for the NotificationForRequest event inside your JavaScript to do something client side.

    Just like this:

    connection.on("NotificationForRequest", function (user, message) {
        // Show the notification.
    });

    More details,you could refer to below article:

    https://docs.microsoft.com/en-us/aspnet/signalr/overview/getting-started/tutorial-getting-started-with-signalr

    Best regards,

    Yijing Sun

    Monday, February 22, 2021 6:51 AM