‫Welcome back.
‫We need to get the message text from this edit text and send it.
‫When the user clicks on this send button to the corresponding and the correct child node in Firebase.
‫So we need to get the message to the correct group to the correct chatting group.
‫For that, I want from you to focus with me because it's very tricky.
‫Open repository and inside the repository I need to create a new function called send message public
‫void send message.
‫And inside this function I need to write the algorithm that send the message to the correct chat group.
‫For that, I need to parameter the message which is of type string and the chat group.
‫In this function I can use the same Firebase instance, but I can't use the database reference because
‫it's as valuable.
‫It depends on the chat group child.
‫So for that I'll create a Firebase database reference equals to database dot get reference.
‫And inside this reference method I need to pass my child my chat group node.
‫Now let's check if the message text is not null.
‫So if message text stream equals to this I'm using this negation operator.
‫So if the message is not null, then I need to send it to the database reference chat message.
‫I need to push object of chat message.
‫So for that I'll create chat message, object, new chat message and inside it I need to pass three
‫parameters.
‫So if we go to the modal chat message we have three parameters sender, id, text, and time.
‫As mind variable will be detected automatically according to the Firebase current user id.
‫The first one is the UID, so I'll get it from firebase auth dot get instance dot get current uid.
‫The second parameter is the message itself, which is passed as a parameter, and the third parameter
‫is the timestamp.
‫So system dot current time in milliseconds.
‫This is my message object.
‫I need to push it to the database reference.
‫But at first we need to create an arbitrary key.
‫So here I'll make a string random key equals to database reference dot push dot.
‫Get key.
‫Here I'm generating a random key.
‫And last step is the reference dot child random key dot.
‫Set value to the message.
‫Here I'm generating a random key for the message.
‫And here I'm setting the value of this key to message of key value.
‫Key value pairs okay.
‫It's very simple.
‫And by the way we learned it about this in the previous videos and in the Firebase introduction.
‫Now let's move to the ViewModel and call this function.
‫So ViewModel I'll create a new message send message method public void send message.
‫In the same way as we done before, we need to pass the same parameters and the same returning type.
‫And here I'm going to call repository dot send message the message itself and the chat group.
‫Again go back to the chat activity.
‫And here I need to handle the click events on the send button.
‫So binding dot and I need to define an ID here to the button.
‫I name it as send button.
‫Back to the chat activity send button dot set on click listener new view on click listener.
‫And by the way, you can write it here by onclick attribute and call the ViewModel instance that we
‫created before the model dot send message.
‫But let me test at first the chat activity binding the send button.
‫The same here I need to call the model or my view model dot send message.
‫And what is the message I need to get it from?
‫The edit text message equals to binding dot edit text chat message dot get text dot to string the message
‫and what is the chat group?
‫The chat group?
‫We get it inside this string, so I need to pass group name.
‫It's very simple.
‫Now we're ready to run our application and test it and see if there is any errors.