コード例 #1
0
        /// <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();
        }
コード例 #2
0
        /// <summary>
        /// validate devices user notification setting and send the notification to iOS and andriod app
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="deviceToken"></param>
        /// <param name="notificationType"></param>
        /// <returns></returns>
        public static bool ValidateAndSendNotification(int userId, string deviceToken, string notificationType, string fullName, string deviceType,
                                                       int totalpendingNotification, byte[] certificate, int totalbudget, string teamName, int teamjoinUserID = 0, int targetID = 0)
        {
            bool                       success             = false;
            StringBuilder              traceLog            = new StringBuilder();
            string                     notificationMessage = string.Empty;
            NotificationType           objNotificationType;
            PushNotificationiOSAndriod objPushNotificationiOSAndriod = null;

            try
            {
                traceLog.AppendLine("Start:ValidateNotificationExits() method");
                if (!Enum.TryParse(notificationType, out objNotificationType))
                {
                    objNotificationType = NotificationType.None;
                }
                int totalNt = 0;
                switch (objNotificationType)
                {
                case NotificationType.TrainerJoinTeam:
                    notificationMessage = string.Format(Message.JoinTeamNotificationMsg, fullName, teamName);
                    break;

                case NotificationType.TrainerChallege:
                    notificationMessage = string.Format(Message.TrainerNotificationMsg, fullName);
                    break;

                case NotificationType.FriendChallege:
                    notificationMessage = string.Format(Message.FriendNotificationMsg, fullName);
                    break;

                case NotificationType.UserSentToReceiver:
                    notificationMessage = string.Format(Message.UserSentToReceiverMsg, fullName);
                    break;

                case NotificationType.NewsFeedBoomed:
                {
                    totalNt = NotificationApiBL.GetALLlNotificationListByResultID(targetID, notificationType, userId);
                    --totalNt;
                    if (totalNt > 0)
                    {
                        notificationMessage = totalNt > 1 ? string.Format(Message.MoreNewsFeedBoomedMsg, fullName, totalNt, Message.NtPeople) :
                                              string.Format(Message.MoreNewsFeedBoomedMsg, fullName, totalNt, Message.NtPerson);
                    }
                    else
                    {
                        notificationMessage = string.Format(Message.NewsFeedBoomedMsg, fullName);
                    }
                    break;
                }

                case NotificationType.NewsFeedCommented:
                {
                    totalNt = NotificationApiBL.GetALLlNotificationListByResultID(targetID, notificationType, userId);
                    --totalNt;
                    if (totalNt > 0)
                    {
                        notificationMessage = totalNt > 1 ? string.Format(Message.MoreNewsFeedCommentedMsg, fullName, totalNt, Message.NtPeople) : string.Format(Message.MoreNewsFeedCommentedMsg, fullName, totalNt, Message.NtPerson);
                    }
                    else
                    {
                        notificationMessage = string.Format(Message.NewsFeedCommentedMsg, fullName);
                    }
                }
                break;

                case NotificationType.ResultFeedBoomed:
                {
                    totalNt = NotificationApiBL.GetALLlNotificationListByResultID(targetID, notificationType, userId);
                    --totalNt;
                    if (totalNt > 0)
                    {
                        notificationMessage = totalNt > 1 ? string.Format(Message.MoreResultBoomedMsg, fullName, totalNt, Message.NtPeople) :
                                              string.Format(Message.MoreResultBoomedMsg, fullName, totalNt, Message.NtPerson);
                    }
                    else
                    {
                        notificationMessage = string.Format(Message.ResultBoomedMsg, fullName);
                    }
                    break;
                }

                case NotificationType.ResultCommented:
                {
                    totalNt = NotificationApiBL.GetALLlNotificationListByResultID(targetID, notificationType, userId);
                    --totalNt;
                    if (totalNt > 0)
                    {
                        notificationMessage = totalNt > 1 ? string.Format(Message.MoreResultCommentedMsg, fullName, totalNt, Message.NtPeople) :
                                              string.Format(Message.MoreResultCommentedMsg, fullName, totalNt, Message.NtPerson);
                    }
                    else
                    {
                        notificationMessage = string.Format(Message.ResultCommentedMsg, fullName);
                    }
                    break;
                }

                case NotificationType.TrainerPostToUser:
                    notificationMessage = string.Format(Message.PersonalTrainerPostToUser, fullName);
                    break;

                case NotificationType.PostCommentedReplyMsg:
                    totalNt = NotificationApiBL.GetALLlNotificationListByResultID(targetID, notificationType, userId);
                    --totalNt;
                    if (totalNt > 0)
                    {
                        notificationMessage = totalNt > 1 ? string.Format(Message.MorePostCommentReplyMsg, fullName, totalNt, Message.NtPeople) :
                                              string.Format(Message.MorePostCommentReplyMsg, fullName, totalNt, Message.NtPerson);
                    }
                    else
                    {
                        notificationMessage = string.Format(Message.PostCommentReplyMsg, fullName);
                    }
                    break;

                case NotificationType.PostResultReplyMsg:
                    totalNt = NotificationApiBL.GetALLlNotificationListByResultID(targetID, notificationType, userId);
                    --totalNt;
                    if (totalNt > 0)
                    {
                        notificationMessage = totalNt > 1 ? string.Format(Message.MoreResultCommentReplyMsg, fullName, totalNt, Message.NtPeople) :
                                              string.Format(Message.MoreResultCommentReplyMsg, fullName, totalNt, Message.NtPerson);
                    }
                    else
                    {
                        notificationMessage = string.Format(Message.ResultCommentReplyMsg, fullName);
                    }
                    break;

                case NotificationType.ProfilesPostToUser:
                    notificationMessage = string.Format(Message.ProfilePostNTMsg, fullName);
                    break;

                case NotificationType.Following:
                    notificationMessage = string.Format(Message.FollowingNTMsg, fullName);
                    break;

                case NotificationType.SelectPrimaryTrainer:
                    notificationMessage = string.Format(Message.UserSelectedPrimaryTrainer, fullName);
                    break;
                }
                objPushNotificationiOSAndriod = new PushNotificationiOSAndriod();
                if (deviceType.Equals(DeviceType.Android.ToString(), StringComparison.OrdinalIgnoreCase))
                {
                    success = objPushNotificationiOSAndriod.SendPushNotificationForAndriod(deviceToken, notificationMessage, notificationType, totalpendingNotification, totalbudget, teamjoinUserID);
                }
                else
                {
                    success = objPushNotificationiOSAndriod.SendPushNotificationForiOS(deviceToken, notificationMessage, notificationType, totalpendingNotification, certificate, totalbudget, teamjoinUserID);
                }
                return(success);
            }
            catch (Exception ex)
            {
                LogManager.LogManagerInstance.WriteErrorLog(ex);
                return(false);
            }
            finally
            {
                objPushNotificationiOSAndriod = null;
                traceLog.AppendLine("End ValidateNotificationExits() : --- " + DateTime.Now.ToLongDateString());
                LogManager.LogManagerInstance.WriteTraceLog(traceLog);
            }
        }