public static async Task<bool> AddChatMessage(SingleChatListModel chatModel) { try { await firebase .Child("Chat").Child(chatModel.sender_user_id.ToString()).Child(chatModel.receiver_user_id.ToString()).PostAsync(chatModel); var chatModel1 = new SingleChatListModel() { is_sender = !chatModel.is_sender, receiver_user_id = chatModel.sender_user_id, sender_user_id = chatModel.receiver_user_id, user_message = chatModel.user_message, user_message_time = chatModel.user_message_time, msg_datetime = chatModel.msg_datetime, file_url = chatModel.file_url, image_url= chatModel.image_url, is_loading = chatModel.is_loading, is_message= chatModel.is_message, message_date_header= chatModel.message_date_header, is_header_visible = chatModel.is_header_visible, file_name = chatModel.file_name, is_image = chatModel.is_image, is_file = chatModel.is_file, time_stamp = chatModel.time_stamp }; await firebase .Child("Chat").Child(chatModel1.sender_user_id.ToString()).Child(chatModel1.receiver_user_id.ToString()).PostAsync(chatModel1); return true; } catch (Exception ex) { Console.WriteLine("AddingChatToFirebase_Exception:- " + ex.Message); return false; } }
public async void AddMessageFirebase(SingleChatListModel item) { try { if (Common.CheckConnection()) { var request = new ChatDetailModelApi() { from_user_id = user_id, to_user_id = RecieverId }; Chatdetailresponse response; try { response = await _webApiRestClient.PostAsync <ChatDetailModelApi, Chatdetailresponse>(ApiUrl.AddChatRequest, request); } catch (Exception ex) { Console.WriteLine("SendMsgApi_Exception:- " + ex.Message); response = null; IsBackPress = true; //await MaterialDialog.Instance.SnackbarAsync(message: AppResource.error_ServerError, msDuration: 3000); return; } if (response != null) { if (response.status) { MessagingCenter.Send("", "ScrollToEnd"); MessageEntry = string.Empty; var data = await FirebaseChatHelper.AddChatMessage(item); } else if (response.message == null) { MessagingCenter.Send("", "ScrollToEnd"); MessageEntry = string.Empty; var data = await FirebaseChatHelper.AddChatMessage(item); } else { await MaterialDialog.Instance.SnackbarAsync(message : response.message, msDuration : 3000); } var msgItem = new SingleChatDBModel() { SingleMessageName = ChatDetailTitle, UserMessagesList = SupportChatList, RecieverId = RecieverId }; if (singleChatMessagesDbService.IsSingleChatPresentInDB()) { var newmsgItem = singleChatMessagesDbService.ReadAllItems().Where(x => x.RecieverId == RecieverId).ToList(); if (newmsgItem != null && newmsgItem.Count > 0) { var msgID = newmsgItem.FirstOrDefault().ID; msgItem.ID = msgID; BsonValue id = new BsonValue(msgID); singleChatMessagesDbService.UpdateSingleChatDataInDb(id, msgItem); } else { singleChatMessagesDbService.CreateSingleChatDataInDB(msgItem); } } else { singleChatMessagesDbService.CreateSingleChatDataInDB(msgItem); } } IsBackPress = true; } else { await MaterialDialog.Instance.SnackbarAsync(message : AppResource.error_InternetError, msDuration : 3000); } } catch (Exception ex) { //await MaterialDialog.Instance.SnackbarAsync(message: ex.Message, // msDuration: 3000); IsBackPress = true; Console.WriteLine("SendMsgCommand_Exception:- " + ex.Message); } }