Exemplo n.º 1
0
 /// <summary>
 /// Upadte or Save DevicesNotification count for user
 /// </summary>
 /// <param name="model"></param>
 /// <returns></returns>
 public static long SaveDevicesNotification(UserNotificationsVM model)
 {
     using (LinksMediaContext _dbContext = new LinksMediaContext())
     {
         StringBuilder traceLog = null;
         try
         {
             traceLog = new StringBuilder();
             traceLog.AppendLine("Start: GetSaveDevicesNotification");
             int userCredetials = 0;
             var objCredentials = _dbContext.Credentials.Where(user => user.UserId == model.UserID && user.UserType.Equals(model.UserType, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
             if (objCredentials != null)
             {
                 userCredetials = objCredentials.Id;
             }
             if (userCredetials != model.ReceiverCredID && !_dbContext.UserNotifications.Any(f => f.SenderCredlID == userCredetials &&
                                                                                             f.SenderCredlID == model.ReceiverCredID && f.ReceiverCredID == model.ReceiverCredID && f.NotificationType == model.NotificationType &&
                                                                                             f.TargetID == model.TargetPostID && f.TokenDevicesID == model.TokenDevicesID))
             {
                 tblUserNotifications objdeviceNft = new tblUserNotifications
                 {
                     SenderCredlID       = userCredetials,
                     ReceiverCredID      = model.ReceiverCredID,
                     NotificationType    = model.NotificationType,
                     SenderUserName      = model.SenderUserName,
                     Status              = true,
                     IsRead              = false,
                     TargetID            = model.TargetPostID,
                     CreatedDate         = model.CreatedNotificationUtcDateTime,
                     TokenDevicesID      = model.TokenDevicesID,
                     ChallengeToFriendId = model.ChallengeToFriendId,
                     IsFriendChallenge   = model.IsFriendChallenge,
                     IsOnBoarding        = model.IsOnBoarding
                 };
                 _dbContext.UserNotifications.Add(objdeviceNft);
                 _dbContext.SaveChanges();
                 return(objdeviceNft.NotificationID);
             }
             return(0);
         }
         finally
         {
             traceLog.AppendLine("End: GetSaveDevicesNotification --- " + DateTime.Now.ToLongDateString());
             LogManager.LogManagerInstance.WriteTraceLog(traceLog);
         }
     }
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="credUserId"></param>
        /// <param name="message"></param>
        private static void SendChatNotification(SocketSentChatVM chatHistory)
        {
            byte[] certificate = File.ReadAllBytes(chatHistory.CeritifcatePath);
            new Thread(() =>
            {
                Thread.CurrentThread.IsBackground = false;
                lock (syncLock)
                {
                    StringBuilder traceLog = null;
                    List <UserNotification> allactiveDevices = null;
                    using (LinksMediaContext dataContext = new LinksMediaContext())
                    {
                        try
                        {
                            traceLog = new StringBuilder();
                            traceLog.Append("Start:SendChatNotification()");
                            string notificationType = string.Empty;
                            traceLog.AppendLine("Start: SendChallegesNotificationToUser()");

                            List <tblCredentials> chatsenderReceiverDetails = dataContext.Credentials.Where(em => string.Compare(em.EmailId, chatHistory.ReceiverEmailId, StringComparison.OrdinalIgnoreCase) == 0 || string.Compare(em.EmailId, chatHistory.SenderEmailId, StringComparison.OrdinalIgnoreCase) == 0).ToList();
                            int receiverCredId    = 0;
                            int senderCredId      = 0;
                            string senderUserType = string.Empty;
                            if (chatsenderReceiverDetails != null)
                            {
                                receiverCredId = chatsenderReceiverDetails.Where(em => em.EmailId.Equals(chatHistory.ReceiverEmailId, StringComparison.OrdinalIgnoreCase)).Select(cr => cr.Id).FirstOrDefault();
                                senderCredId   = chatsenderReceiverDetails.Where(em => em.EmailId.Equals(chatHistory.SenderEmailId, StringComparison.OrdinalIgnoreCase)).Select(cr => cr.Id).FirstOrDefault();
                                senderUserType = chatsenderReceiverDetails.Where(em => em.EmailId.Equals(chatHistory.SenderEmailId, StringComparison.OrdinalIgnoreCase)).Select(cr => cr.UserType).FirstOrDefault();
                            }

                            if (chatHistory != null && receiverCredId > 0 && senderCredId > 0 && !string.IsNullOrEmpty(chatHistory.Message))
                            {
                                bool isRead = false;
                                if (!chatHistory.IsOffine)
                                {
                                    isRead = true;
                                }
                                tblChatHistory objChatHistory = new tblChatHistory()
                                {
                                    ReceiverCredId     = receiverCredId,
                                    SenderCredId       = senderCredId,
                                    Message            = chatHistory.Message,
                                    IsRead             = isRead,
                                    TrasactionDateTime = Convert.ToDateTime(chatHistory.TrasactionDateTime),
                                };
                                dataContext.ChatHistory.Add(objChatHistory);
                                dataContext.SaveChanges();
                                if (chatHistory.IsOffine)
                                {
                                    string message         = string.Empty;
                                    allactiveDevices       = PushNotificationBL.GetLastUserDeviceID(receiverCredId);
                                    string senderFirstName = string.Empty;
                                    if (senderUserType.Equals(Message.UserTypeUser, StringComparison.OrdinalIgnoreCase))
                                    {
                                        senderFirstName = (from usr in dataContext.User
                                                           join crd in dataContext.Credentials
                                                           on usr.UserId equals crd.UserId
                                                           where crd.Id == senderCredId && crd.UserType == Message.UserTypeUser
                                                           select usr.FirstName + " " + usr.LastName).FirstOrDefault();
                                    }
                                    else if (senderUserType.Equals(Message.UserTypeTrainer, StringComparison.OrdinalIgnoreCase))
                                    {
                                        senderFirstName = (from usr in dataContext.Trainer
                                                           join crd in dataContext.Credentials
                                                           on usr.TrainerId equals crd.UserId
                                                           where crd.Id == senderCredId && crd.UserType == Message.UserTypeTrainer
                                                           select usr.FirstName + " " + usr.LastName).FirstOrDefault();
                                    }
                                    message = senderFirstName + " sent you a message!";
                                    bool isSaveSuccessfully = false;
                                    foreach (UserNotification objuserNotification in allactiveDevices)
                                    {
                                        // If Deives Token null then by pass to sending push notification
                                        if (!string.IsNullOrEmpty(objuserNotification.DeviceID))
                                        {
                                            if (!isSaveSuccessfully)
                                            {
                                                notificationType = ConstantHelper.constChatNotification;
                                                tblUserNotifications objdeviceNft = new tblUserNotifications
                                                {
                                                    SenderCredlID    = senderCredId,
                                                    ReceiverCredID   = receiverCredId,
                                                    NotificationType = notificationType,
                                                    SenderUserName   = senderFirstName,
                                                    Status           = true,
                                                    IsRead           = false,
                                                    TargetID         = 0,
                                                    CreatedDate      = DateTime.Parse(chatHistory.TrasactionDateTime),
                                                    TokenDevicesID   = objuserNotification.DeviceID
                                                };
                                                dataContext.UserNotifications.Add(objdeviceNft);
                                                dataContext.SaveChanges();
                                                isSaveSuccessfully = true;
                                            }
                                            PushNotificationiOSAndriod objPushNotificationiOSAndriod = new PushNotificationiOSAndriod();
                                            int totalNotificationcount = dataContext.ChatHistory.Count(ch => ch.ReceiverCredId == receiverCredId && ch.IsRead == false);
                                            int totalbudget            = CommonWebApiBL.GetTotalUSerNotification(receiverCredId, objuserNotification.DeviceID);
                                            if (objuserNotification.DeviceType.Equals(DeviceType.IOS.ToString(), StringComparison.OrdinalIgnoreCase))
                                            {
                                                objPushNotificationiOSAndriod.SendPushNotificationForiOS(objuserNotification.DeviceID, message, notificationType, totalNotificationcount, certificate, totalbudget, 0);
                                            }
                                            else if (objuserNotification.DeviceType.Equals(DeviceType.Android.ToString(), StringComparison.OrdinalIgnoreCase))
                                            {
                                                objPushNotificationiOSAndriod.SendPushNotificationForAndriod(objuserNotification.DeviceID, message, notificationType, totalNotificationcount, totalbudget, 0);
                                            }
                                        }
                                    }
                                    message = string.Empty;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            LogManager.LogManagerInstance.WriteErrorLog(ex);
                        }
                        finally
                        {
                            traceLog.AppendLine("End: SendChatNotification()  --- " + DateTime.Now.ToLongDateString());
                            LogManager.LogManagerInstance.WriteTraceLog(traceLog);
                            traceLog         = null;
                            allactiveDevices = null;
                        }
                    }
                }
            }).Start();
        }