Once the application for Android talk starts, it needs to verify that the user was signed in
Step One: Handle User Sign-In
In that case, the app should go in advance and highlight the contents of the chat space. Normally, it should reroute the consumer to either a sign-in monitor or a sign-up monitor. With FirebaseUI, creating those displays requires much less code than you might envision.
Within the onCreate() approach to MainActivity , verify that an individual is signed in. To do this, find out if the existing FirebaseUser item is not null . Whether it’s null , you need to make and arrange an Intent item that starts a sign-in task. To do so, use the SignInIntentBuilder class. As soon as purpose is prepared, you need to introduce the sign-in task with the startActivityForResult() approach.
Keep in mind that the sign-in task additionally permits new users to register. Therefore, you don’t have to create any added laws to manage consumer subscription.
As you care able to see within the above code, if consumer has already been closed in, we initially show a Toast welcoming an individual, following call a technique known as displayChatMessages. For the time being, merely write a stub for it. We’re going to be incorporating laws to they afterwards.
After the consumer keeps signed in, MainActivity will receive a consequence by means of an Intent . To deal with it, you need to bypass the onActivityResult() way.
If consequences’s signal are RESULT_OK , it indicates an individual enjoys finalized in successfully. If yes, it is vital that you name the displayChatMessages() strategy once again. Or else, call finish() to close off the app.
Step 2: Manage User Sign-Out
By default, FirebaseUI uses practical Lock for Passwords. Thus, once the consumers register, they are going to remain finalized in even if the software try restarted. Permitting the consumers to signal away, we’re going to now incorporate a sign-out substitute for the overflow eating plan of MainActivity .
Generate a unique selection source file known as main_menu.xml and add one item to it, whose subject feature is scheduled to signal out. The belongings in the file need to look such as this:
To instantiate the eating plan source inside MainActivity , override the onCreateOptionsMenu() system. After that contact the inflate() way of the MenuInflater item.
Subsequent, override the onOptionsItemSelected() approach to manage click events regarding diet plan items. In the means, possible name the signOut() technique of the AuthUI course to signal the user completely. Due to the fact sign-out process are executed asynchronously, we are going to also add an OnCompleteListener to it.
Once the user have signed completely, the software should close automatically. This is why precisely why you discover a phone call toward finish() process inside the signal above.
5. Establish A Product
So that you can store the chat emails cupid inside Firebase real time databases, you must establish a design on their behalf. The layout for the chat message, which we created earlier on within tutorial, provides three vista. To populate those horizon, the product as well need at least three fields.
Build a Java lessons known as ChatMessage.java and include three representative variables to it: messageText , messageUser , and messageTime . In addition, include a constructor to initialize those factors.
To help make the product appropriate for FirebaseUI, it is vital that you include a default constructor to they, alongside getters and setters for all the representative variables.
6. Post A Chat Message
Inside the listener, you must very first become a DatabaseReference object with the getReference() way of the FirebaseDatabase lessons. You’ll be able to contact the push() and setValue() techniques to add brand new cases of the ChatMessage lessons towards the real time databases.
The ChatMessage instances must, needless to say, getting initialized making use of the belongings in the EditText in addition to screen label of currently signed-in individual.