Step 1: Handle Individual Sign-In
If that’s the case, the software is going ahead and showcase the contents of the speak area. If not, it must redirect the user to either a sign-in monitor or a sign-up display. With FirebaseUI, generating those displays takes much less code than you may imagine.
Inside onCreate() method of MainActivity , verify that the consumer is already signed in. To do this, find out if current FirebaseUser object just isn’t null . If it’s null , it is vital that you make and configure an Intent object that starts a sign-in activity. To achieve this, use the SignInIntentBuilder course. When the intent is prepared, you have to start the sign-in activity with the startActivityForResult() means.
Note that the sign-in task furthermore allows new users to join up. Consequently, you don’t have to create any extra signal to carry out individual enrollment .
Perhaps you have realized in above code, if user has already been signed in, we 1st showcase a Toast inviting an individual, following contact a method called displayChatMessages. For the time being, only build a stub because of it. We are going to feel incorporating code to they later on.
Once the user possess signed in, MainActivity will receive a consequence by means of an intention . To take care of it, you must override the onActivityResult() system.
If the outcome’s laws is actually RESULT_OK , it means an individual possess signed in successfully. If yes, you have to name the displayChatMessages() process once again. Otherwise, contact finish() to close off the application.
Step 2: Manage User Sign-Out
By default, FirebaseUI utilizes Intelligent Lock for Passwords. Therefore, when the people register, they’re going to remain signed in even when the application is restarted. Allowing the consumers to signal around, we’re going to today add a sign-out choice to the overflow diet plan of MainActivity .
Create a diet plan site file known as main_menu.xml and put an individual product to they, whoever name feature is scheduled to signal out. The belongings in the document will want to look like this:
To instantiate the diet plan source inside MainActivity , override the onCreateOptionsMenu() means. Then name the inflate() technique of the MenuInflater item.
Next, override the onOptionsItemSelected() method to deal with click occasions from the selection product. Inside the approach, you’ll be able to phone the signOut() method of the AuthUI course to sign the consumer on. Because sign-out procedure was accomplished asynchronously, we are going to also add an OnCompleteListener to it.
Once the individual provides closed out, the application should shut immediately. That’s the reason exactly why you discover a phone call towards finish() process inside rule over.
5. Produce A Design
In order to save the chat messages in Firebase realtime databases, it is vital that you create a model on their behalf. The design in the chat message, which we developed earlier on inside information, has three vista. To be able to populate those horizon, the unit as well need to have at the very least three areas.
Generate a Java class also known as ChatMessage.java and create three representative factors to it: messageText , messageUser , and messageTime . In addition, add a constructor to initialize those variables.
To really make the design compatible with FirebaseUI, you have to also add a default constructor to they, alongside getters and setters for the user factors.
6. Post A Speak Content
Inside the listener, it is vital that you initially become a DatabaseReference item utilizing the getReference() technique of the FirebaseDatabase lessons. After that you can name the push() and setValue() methods to incorporate newer instances of the ChatMessage course into the real time databases.
The ChatMessage cases must, without a doubt, end up being initialized making use of the contents of the EditText plus the show identity in the at this time signed-in consumer.