private void SendNotification(string sendTo, string username, string image, string message,Boolean storeDB, string controller, string action, string param) { var context = GlobalHost.ConnectionManager.GetHubContext<notifications>(); //var name = Context.User.Identity.Name; using (var db = new ApplicationDbContext()) { var user = db.notifyConnections.Where(c => c.userName == sendTo).FirstOrDefault(); if (user != null) { string finalmessage = image + "^" + username + "^" + message; string link = controller + "/" + action + "?" + param; if (storeDB == true) { Notifications notify = new Notifications(); notify.ID = Guid.NewGuid(); notify.isRead = false; notify.Message = finalmessage; notify.UserName = sendTo; notify.postedTime = DateTime.Now; notify.link = link; db.notifications.Add(notify); db.SaveChanges(); } var counter = db.notifications.Where(c => c.UserName == sendTo && c.isRead==false).Count(); finalmessage = finalmessage + "^" + counter+"^"+link; context.Clients.Client(user.connectionId) .recieverNotifier(finalmessage); } } }
public void SendChatStudentReiever(string sessionId, string sendTo, string message, IHubContext context) { //var name = Context.User.Identity.Name; using (var db = new ApplicationDbContext()) { string link = "Students/Sessions" + "?SessionId=" + sessionId; var user = db.Useras.Where(c => c.UserName == sendTo && c.SessionId == sessionId).FirstOrDefault(); //for notification var session = db.sessions.Where(c => c.SessionID == new Guid(sessionId)).FirstOrDefault(); var Username = session.tutor.Username; var img = session.tutor.ProfileImage; if (user == null) { // context.Clients.Caller.showErrorMessage("Could not find that user."); var notiAlready = db.notifications.Where(c => c.sessionId == sessionId && c.UserName == sendTo).FirstOrDefault(); if (notiAlready == null) { Notifications notify = new Notifications(); notify.ID = Guid.NewGuid(); notify.isRead = false; notify.Message = img + "^" + Username + "^" + "has sent you a message."; notify.UserName = sendTo; notify.sessionId = sessionId; notify.postedTime = DateTime.Now; notify.counts = 1; notify.link = link; db.notifications.Add(notify); } else { notiAlready.counts = notiAlready.counts + 1; notiAlready.isRead = false; notiAlready.postedTime = DateTime.Now; db.Entry(notiAlready).State = EntityState.Modified; } db.SaveChanges(); SendNotification(sendTo, session.tutor.Username, session.tutor.ProfileImage, "has sent you a message.", false, "Students", "Sessions", "SessionId=" + sessionId); } else { db.Entry(user) .Collection(u => u.Connections) .Query() .Where(c => c.Connected == true) .Load(); if (user.Connections == null) { var notiAlready = db.notifications.Where(c => c.sessionId == sessionId && c.UserName == sendTo).FirstOrDefault(); if (notiAlready == null) { Notifications notify = new Notifications(); notify.ID = Guid.NewGuid(); notify.isRead = false; notify.Message = img + "^" +Username+ "^"+ "has sent you a message."; notify.UserName = sendTo; notify.sessionId = sessionId; notify.postedTime = DateTime.Now; notify.link = link; db.notifications.Add(notify); } else { notiAlready.counts = notiAlready.counts + 1; notiAlready.isRead = false; notiAlready.postedTime = DateTime.Now; db.Entry(notiAlready).State = EntityState.Modified; } db.SaveChanges(); SendNotification(sendTo, session.tutor.Username, session.tutor.ProfileImage, "has sent you a message.",false, "Students", "Sessions", "SessionId=" + sessionId); } else { foreach (var connection in user.Connections) { context.Clients.Client(connection.ConnectionID) .recieverTutor(message); } } } } }
public async void SendChatMessageTutorReciever(string sessionId, string sendTo, string message, IHubContext context) { //var name = Context.User.Identity.Name; using (var db = new ApplicationDbContext()) { var userWithSession = db.Useras.Where(c => c.UserName == sendTo && c.SessionId == sessionId).FirstOrDefault(); var Online = db.online.Where(c => c.Username == sendTo).FirstOrDefault().Status; //if not online send eamail if (Online == false) { var email = db.Users.Where(c => c.UserName == sendTo).FirstOrDefault().Email; String subject = "New message for you on MezoExperts.com"; String bodyText = sendTo + " sent you a message on MezoExperts."; string FileTemplate = "passwordreset.html"; await sendEmail(email, subject, bodyText, FileTemplate, ""); } //if user was not on session if (userWithSession == null) { // context.Clients.Caller.showErrorMessage("Could not find that user."); var session = db.sessions.Where(c => c.SessionID == new Guid(sessionId)).FirstOrDefault(); var Username = session.question.student.Username; var img = session.question.student.ProfileImage; var notiAlready = db.notifications.Where(c => c.sessionId == sessionId && c.UserName == sendTo).FirstOrDefault(); string link = "Tutors/Sessions" + "?SessionId=" + sessionId; if (notiAlready == null) { Notifications notify = new Notifications(); notify.ID = Guid.NewGuid(); notify.isRead = false; notify.Message = session.question.student.ProfileImage + "^" + Username + "^" + "has sent you a message."; notify.UserName = sendTo; notify.sessionId = sessionId; notify.postedTime = DateTime.Now; notify.link = link; notify.counts = 1; db.notifications.Add(notify); } else { notiAlready.counts = notiAlready.counts + 1; notiAlready.isRead = false; notiAlready.postedTime = DateTime.Now; db.Entry(notiAlready).State = EntityState.Modified; } db.SaveChanges(); SendNotification(sendTo, Username, session.question.student.ProfileImage, "has sent you a message.", false, "Tutors", "Sessions", "SessionId=" + sessionId); } else { db.Entry(userWithSession) .Collection(u => u.Connections) .Query() .Where(c => c.Connected == true) .Load(); if (userWithSession.Connections == null) { var session = db.sessions.Where(c => c.SessionID == new Guid(sessionId)).FirstOrDefault(); var Username = session.question.student.Username; var img = session.question.student.ProfileImage; string link = "Tutors/Sessions" + "?SessionId=" + sessionId; var notiAlready = db.notifications.Where(c => c.sessionId == sessionId && c.UserName == sendTo).FirstOrDefault(); if (notiAlready == null) { Notifications notify = new Notifications(); notify.ID = Guid.NewGuid(); notify.isRead = false; notify.Message = session.question.student.ProfileImage+"^"+Username + "^" + "has sent you a message."; notify.UserName = sendTo; notify.sessionId = sessionId; notify.postedTime = DateTime.Now; notify.link = link; db.notifications.Add(notify); } else { notiAlready.counts = notiAlready.counts + 1; notiAlready.isRead = false; notiAlready.postedTime = DateTime.Now; db.Entry(notiAlready).State = EntityState.Modified; } db.SaveChanges(); SendNotification(sendTo, Username, session.question.student.ProfileImage, "has sent you a message.",false, "Tutors", "Sessions", "SessionId=" + sessionId); } else { foreach (var connection in userWithSession.Connections) { context.Clients.Client(connection.ConnectionID) .recieverStudent2(message); } } } } }