'SignalR push notification doesn't show

In my web application, I want to add push notifications to send alerts to the users. I have never tried this before and followed some tutorials..

In my project I configured my db connection in a class like this.

public class zSqlDb: DbContext {
    public zSqlDb(): base("Data Source=YENUKA-LAPTOP;Initial Catalog=db_ptweb;Integrated Security=True")
    {
    }
}

But in the tutorials I watched they put the connection string on the web config file and then they create a class and wrote SQL condition to write the changes on the table.

Here within the control is where I wrote to save the change in the notification table.

  PushNotification pushNotification = new PushNotification();
  pushNotification.ReqId = appRequest.Id;
  pushNotification.RUserId = appRequest.Create_By;
  pushNotification.SUserId = appRequest.ApprovalPartyList.First().Approver_Id;
  pushNotification.Message = "You have a request to approve";
  pushNotification.CreatedDate = DateTime.Today;
  pushNotification.Status = true;
  db.PushNotification.Add(pushNotification);
  db.SaveChanges();

Is it okay ?

To retrieve the unread notification I wrote jQuery like this in the main view

    < script type = "text/javascript" >
      $(function () {

        $('span.noti').click(function (e) {
          e.stopPropagation();
          $('.noti-content').show();
          var count = 0;
          count = parseInt($('span.count').html()) || 0;
          //only load notification if not already loaded
          if (count > 0) {
            updateNofification();
          }
          $('span.count', this).html('&nbsp;');
        })
        //hide notification
        $('html').click(function () {
          $('.noti-content').hide();
        })
        //update notification

        $(document).ready(function () {
          updateNofification();
          console.log("ready!");
        });

        function updateNofification() {
          $('#notiContent').empty();
          $('#notiContent').append($('<li> Loading.. </li>'));

          $.ajax({

            type: 'GET',
            url: '/Home/GetNotification',

            sucess: function (response) {

              $('notiContent').empty();
              if (response.length == 0) {
                $('#notiContent').append($('<li>No data available</li>'));
                console.log("No Data");
              }
              $.each(response, function (index, value) {
                $('#notiContent').append($('<li> New contat :' + value.ReqId + '(' + value.EmpName + ')added</li>'));
                console.log(value.ReqId);
              });

            },

            error: function (error) {
              console.log(error);
              alert(error);
            }

          })
        }
        //update notification count
        function updateNotificationCount() {
          var count = 0;
          count = parseInt($('span.count').html()) || 0;
          count++;
          $('span.count').html(count);
        }
        // signalr js code for start hub and send recive notification
        var notificationHub = $.connection.Notifications;
        $.connection.hub.start().done(function () {
          console.log('notification hub started');

        });

        ////signalr method for push server message to client
        //Notifications.client.notify = function (message) {
        //    if (message && message.toLowerCase() == "added") {
        //        updateNotificationCount();
        //    }
        //}
      }) <
      /script>

This is the Home GetNotification code

public JsonResult GetNotification() {
  return Json(NotificaionService.GetNotification(), JsonRequestBehavior.AllowGet);
}

This is the GetNotification Code

public static class NotificaionService {
  private static zSqlDb db = new zSqlDb();

  internal static SqlCommand command = null;
  internal static SqlDependency dependency = null;

  public static string GetNotification() {
    try {

      var messages = new List < PushNotification > ();

      List < PushNotification > ActiveNoti = db.PushNotification.Where(x => x.Status == true).ToList();

      var jsonSerialiser = new JavaScriptSerializer();
      var json = jsonSerialiser.Serialize(messages);
      return json;

    } catch (Exception ex) {

      return null;
    }

  }
}

This is my HUB

[HubName("notifications")]
public class Notifications: Hub {

  public static void Show() {
    IHubContext context = GlobalHost.ConnectionManager.GetHubContext < Notifications > ();
    context.Clients.All.displayNoti();
  }
}

Even though there are records in the notification table, they are not showing the notifications. Can you help me on this? Also is there any other way to allow push notification to the web application? This is I followed tutorial

http://www.dotnetawesome.com/2016/05/push-notification-system-with-signalr.html



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source