public HttpResponseMessage SendMessageGroup([FromBody] ChatGroup chat) { var accountSend = db.ACCOUNTs.SingleOrDefault(x => x.UserName == chat.data.UserName); var group = db.MESSAGING_GROUP.SingleOrDefault(x => x.MESSAGING_GROUP_ID == chat.data.MESSAGING_GROUP_ID); var conference = db.CONFERENCEs.SingleOrDefault(x => x.CONFERENCE_ID == chat.data.CONFERENCE_ID); if (accountSend == null) { return(ResponseFail(StringResource.Account_does_not_exist)); } else if (group == null) { return(ResponseFail(StringResource.Message_group_do_not_exist)); } else if (conference == null) { return(ResponseFail(StringResource.Conference_do_not_exist)); } else { var listMember = db.ACCOUNT_MESSAGING_GROUP_MEMBERSHIP.Where(x => x.MESSAGING_GROUP_ID == chat.data.MESSAGING_GROUP_ID && (x.GROUP_ASSIGNED_OR_GROUP_JOIN_REQUEST_OR_GROUP_JOIN_REQUEST_APPROVED == "GROUP ASSIGNED" || x.GROUP_ASSIGNED_OR_GROUP_JOIN_REQUEST_OR_GROUP_JOIN_REQUEST_APPROVED == "GROUP JOIN REQUEST APPROVED") && (x.DELETED == false || x.DELETED == null)).ToList(); var adminGroup = new ACCOUNT_MESSAGING_GROUP_MEMBERSHIP(); adminGroup.UserName = group.CREATED_UserName; listMember.Add(adminGroup); /*Thêm vào CSDL*/ var messageFeed = new MESSAGE_FEED(); messageFeed.FROM_DATE = DateTime.Now; messageFeed.UserName = chat.data.UserName; messageFeed.CURRENT_FIRST_NAME = accountSend.CURRENT_FIRST_NAME; messageFeed.CURRENT_MIDDLE_NAME = accountSend.CURRENT_MIDDLE_NAME; messageFeed.CURRENT_LAST_NAME = accountSend.CURRENT_LAST_NAME; messageFeed.CONFERENCE_ID = chat.data.CONFERENCE_ID; messageFeed.CONFERENCE_NAME = conference.CONFERENCE_NAME; messageFeed.CONFERENCE_NAME_EN = conference.CONFERENCE_NAME_EN; messageFeed.PUBLIC_OR_PRIVATE_MESSAGE = "PRIVATE"; messageFeed.MESSAGE_CONTENT = chat.Body; messageFeed.RECIPIENT_MESSAGING_GROUP_ID_1 = group.MESSAGING_GROUP_ID; messageFeed.RECIPIENT_MESSAGING_GROUP_NAME_1 = group.MESSAGING_GROUP_NAME; messageFeed.RECIPIENT_MESSAGING_GROUP_NAME_EN_1 = group.MESSAGING_GROUP_NAME; new NewFeedModel().addNewFeed(messageFeed); /*Tạo dữ liệu trả về*/ var msg = new MessageResponse(); msg.MESSAGE_FEED_ID = messageFeed.MESSAGE_FEED_ID; msg.MESSAGE_CONTENT = messageFeed.MESSAGE_CONTENT; msg.IsMoreThanOneDay = false; msg.IsToDay = DateTimeFormater.CheckIsToday(messageFeed.FROM_DATE.Value); msg.TimeFormat = DateTimeFormater.GetTimeAgo(messageFeed.FROM_DATE.Value); msg.Time = messageFeed.FROM_DATE.Value; msg.UserName = messageFeed.UserName; msg.PERSON_ID_SEND = accountSend.PERSON_ID; msg.ORGANIZATION_SEND = accountSend.CURRENT_HOME_ORGANIZATION_NAME; msg.ORGANIZATION_EN_SEND = accountSend.CURRENT_HOME_ORGANIZATION_NAME_EN; msg.Image = accountSend.Image; msg.FULL_NAME_PERSON_SEND = Utils.GetFullName(messageFeed.CURRENT_FIRST_NAME, messageFeed.CURRENT_MIDDLE_NAME, messageFeed.CURRENT_LAST_NAME); msg.RECIPIENT_MESSAGING_GROUP_ID_1 = messageFeed.RECIPIENT_MESSAGING_GROUP_ID_1; msg.RECIPIENT_MESSAGING_GROUP_NAME_1 = messageFeed.RECIPIENT_MESSAGING_GROUP_NAME_1; msg.RECIPIENT_MESSAGING_GROUP_NAME_EN_1 = messageFeed.RECIPIENT_MESSAGING_GROUP_NAME_EN_1; msg.IS_MESSAGE_GROUP = true; foreach (ACCOUNT_MESSAGING_GROUP_MEMBERSHIP item in listMember) { var memberRecive = db.ACCOUNT_DEVICE_RELATIONSHIP.SingleOrDefault(x => x.UserName == item.UserName); if (memberRecive != null) { var deviceId = memberRecive.DEVICE_TOKEN; if (deviceId != null) { var result = "-1"; var webAddr = "https://fcm.googleapis.com/fcm/send"; var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr); httpWebRequest.ContentType = "application/json"; httpWebRequest.Headers.Add("Authorization:key=" + StringResource.Server_fcm_key); httpWebRequest.Method = "POST"; using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { var notification = new Notification(); notification.body = chat.Body; notification.title = msg.FULL_NAME_PERSON_SEND; notification.sound = chat.Sound; notification.priority = chat.Priority; JsonData data = new JsonData(); data.Data = JsonConvert.SerializeObject(msg); var json = new DataJson(); json.notification = notification; json.data = data; json.to = deviceId; streamWriter.Write(JsonConvert.SerializeObject(json)); streamWriter.Flush(); var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { result = streamReader.ReadToEnd(); } } } } } return(ResponseSuccess(StringResource.Success, msg)); } }
public HttpResponseMessage SendMessage([FromBody] Chat chat) { if (chat != null) { try { var accountSend = db.ACCOUNTs.SingleOrDefault(x => x.UserName == chat.data.UserName); var accountRecive = db.ACCOUNTs.SingleOrDefault(x => x.UserName == chat.data.RECIPIENT_UserName_1); var conference = db.CONFERENCEs.SingleOrDefault(x => x.CONFERENCE_ID == chat.data.CONFERENCE_ID); var manage = new ManageDeviceModel().GetDeviceUserName(chat.data.RECIPIENT_UserName_1); if (accountSend == null) { return(ResponseFail(StringResource.Account_does_not_exist)); } else if (accountRecive == null) { return(ResponseFail(StringResource.Account_does_not_exist)); } else if (conference == null) { return(ResponseFail(StringResource.Conference_do_not_exist)); } else if (manage == null) { return(ResponseFail(StringResource.Account_has_not_been_logged)); } else if (String.IsNullOrEmpty(manage.DEVICE_TOKEN)) { return(ResponseFail(StringResource.Account_has_not_been_logged)); } else { string deviceId = manage.DEVICE_TOKEN; var result = "-1"; var webAddr = "https://fcm.googleapis.com/fcm/send"; var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr); httpWebRequest.ContentType = "application/json"; httpWebRequest.Headers.Add("Authorization:key=" + StringResource.Server_fcm_key); httpWebRequest.Method = "POST"; /*Thêm vào CSDL*/ var messageFeed = new MESSAGE_FEED(); messageFeed.FROM_DATE = DateTime.Now; messageFeed.UserName = chat.data.UserName; messageFeed.CURRENT_FIRST_NAME = accountSend.CURRENT_FIRST_NAME; messageFeed.CURRENT_MIDDLE_NAME = accountSend.CURRENT_MIDDLE_NAME; messageFeed.CURRENT_LAST_NAME = accountSend.CURRENT_LAST_NAME; messageFeed.CONFERENCE_ID = chat.data.CONFERENCE_ID; messageFeed.CONFERENCE_NAME = conference.CONFERENCE_NAME; messageFeed.CONFERENCE_NAME_EN = conference.CONFERENCE_NAME_EN; messageFeed.PUBLIC_OR_PRIVATE_MESSAGE = "PRIVATE"; messageFeed.MESSAGE_CONTENT = chat.Body; messageFeed.RECIPIENT_UserName_1 = chat.data.RECIPIENT_UserName_1; messageFeed.RECIPIENT_CURRENT_LAST_NAME_1 = accountRecive.CURRENT_LAST_NAME; messageFeed.RECIPIENT_CURRENT_MIDDLE_NAME_1 = accountRecive.CURRENT_MIDDLE_NAME; messageFeed.RECIPIENT_CURRENT_FIRST_NAME_1 = accountRecive.CURRENT_FIRST_NAME; new NewFeedModel().addNewFeed(messageFeed); /*Tạo dữ liệu trả về*/ var msg = new MessageResponse(); msg.MESSAGE_FEED_ID = messageFeed.MESSAGE_FEED_ID; msg.MESSAGE_CONTENT = messageFeed.MESSAGE_CONTENT; msg.IsMoreThanOneDay = false; msg.IsToDay = DateTimeFormater.CheckIsToday(messageFeed.FROM_DATE.Value); msg.TimeFormat = DateTimeFormater.GetTimeAgo(messageFeed.FROM_DATE.Value); msg.Time = messageFeed.FROM_DATE.Value; msg.UserName = messageFeed.UserName; msg.PERSON_ID_SEND = accountSend.PERSON_ID; msg.ORGANIZATION_SEND = accountSend.CURRENT_HOME_ORGANIZATION_NAME; msg.ORGANIZATION_EN_SEND = accountSend.CURRENT_HOME_ORGANIZATION_NAME_EN; msg.Image = accountSend.Image; msg.FULL_NAME_PERSON_SEND = Utils.GetFullName(messageFeed.CURRENT_FIRST_NAME, messageFeed.CURRENT_MIDDLE_NAME, messageFeed.CURRENT_LAST_NAME); msg.RECIPIENT_UserName_1 = messageFeed.RECIPIENT_UserName_1; msg.FULL_NAME_PERSON_RECIVE = Utils.GetFullName(messageFeed.RECIPIENT_CURRENT_FIRST_NAME_1, messageFeed.RECIPIENT_CURRENT_MIDDLE_NAME_1, messageFeed.RECIPIENT_CURRENT_LAST_NAME_1); msg.IS_MESSAGE = true; using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { var notification = new Notification(); notification.body = chat.Body; notification.title = msg.FULL_NAME_PERSON_SEND; notification.sound = chat.Sound; notification.priority = chat.Priority; JsonData data = new JsonData(); data.Data = JsonConvert.SerializeObject(msg); var json = new DataJson(); json.notification = notification; json.data = data; json.to = deviceId; streamWriter.Write(JsonConvert.SerializeObject(json)); streamWriter.Flush(); var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { result = streamReader.ReadToEnd(); } return(ResponseSuccess(StringResource.Success, msg)); } } } catch (Exception) { return(ResponseFail(StringResource.Sorry_an_error_has_occurred)); } } else { return(ResponseFail(StringResource.Data_not_received)); } }