Before 2 months ago I have asked to add Push Notifications to my web application. Still, I didn't figure it out completely yet. Really sorry for asking the same again and again here.
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 to make it done.
I also have some doubts. In my project, I configured my DB connection in a class like this.
public class zSqlDb: DbContext {
public zSqlDb(): base("Data Source=YKA-LAPTOP;Initial Catalog=db_ptweb;Integrated Security=True")
{
}
}
But the tutorials I watched, they put the connection string on the web config file and then they create a class and wrote SQL conditions to write the changes on the table.
But here within the control, 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 the jQuery as 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(' ');
})
//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');
});
})
</script>
This is the Home GetNotification code
public JsonResult GetNotification() {
return Json(NotificaionService.GetNotification(), JsonRequestBehavior.AllowGet);
}
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 there are records in the notification table, not show the notifications. Can you help me with 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
Thanks