4

I have a news site that is powered by C# ASP.Net MVC 5.0 and I want to push notification to clients browser when an admin add/edit or delete a news.
so I added a signalr script to my template which is used in all pages.

_Layout.cshtml

var notificationsHub = $.connection.notificationHub;
$.connection.hub.start();
jQuery(function($) {
    notificationsHub.client.Toast = function (title, body) {
        var options = {
            body: body,
        };
        var notification = new Notification(title, options);
    };
});

and added this code to the page that redirects after news add/edit/delete is done

<script>
    $.connection.hub.start().done(function() {
        notificationsHub.server.toast('@NotificationHub.Type.ToString()', '@Html.Raw(NotificationHub.Title)', '@Html.Raw(NotificationHub.Body)');
    });
</script>

when I add/edit/delete a news it works fine but the problem is it pushes notification to all opened tabs in my site. is there a trick to avoid this happening and show only one browser notification per site?

mhesabi
  • 1,140
  • 3
  • 22
  • 48

2 Answers2

0

Then you have to know the number of open tabs and the URL used. See the "How to get the number of tabs opened of my web in a browser using jquery?" SO question for a discussion that this is not desired for privacy reasons.

Community
  • 1
  • 1
Jeroen Heier
  • 3,520
  • 15
  • 31
  • 32
0

OK I found answer in this SO question.
The trick is to use tag in options:

var notificationsHub = $.connection.notificationHub;
$.connection.hub.start();
jQuery(function($) {
    notificationsHub.client.Toast = function (title, body) {
        var options = {
            body: body,
            tag: 'my-unique-id',
        };
        var notification = new Notification(title, options);
    };
});
Community
  • 1
  • 1
mhesabi
  • 1,140
  • 3
  • 22
  • 48