1: Handle Individual Sign-In
If so, the app is going ahead and showcase the contents of the talk area. Otherwise, it needs to reroute the user to either a sign-in monitor or a sign-up display screen. With FirebaseUI, promoting those displays requires much less code than you possibly might picture.
Inside the onCreate() method of MainActivity , check if the consumer has already been signed in. To work on this, check if the present FirebaseUser item isn’t null . Whether it’s null , it is vital that you write and arrange an Intent object that starts a sign-in activity. To take action, utilize the SignInIntentBuilder class. As soon as purpose is ready, you need to start the sign-in task making use of the startActivityForResult() means.
Keep in mind that the sign-in task furthermore enables new users to join up. For that reason, you don’t have to create any added signal to handle consumer registration.
As you can tell inside the preceding code, if the consumer has already been signed in, we initially highlight a Toast pleasant the user, then phone a way named displayChatMessages. For the present time, merely make a stub for this. We will be including code to they later.
As soon as consumer has actually closed in, MainActivity will get a consequence by means of an intention . To manage it, you have to override the onActivityResult() means.
In the event the outcome’s laws was RESULT_OK , it indicates an individual features closed in successfully. If that’s the case, you have to phone the displayChatMessages() method once more. Otherwise, contact finish() to close the application.
Step Two: Manage Individual Sign-Out
Automatically, FirebaseUI uses Smart Lock for Passwords. Consequently, once the users check in, they’ll stay closed in even if the application try restarted. Permitting the users to signal down, we’re going to now add a sign-out choice to the overflow diet plan of MainActivity .
Create a fresh selection source file labeled as main_menu.xml and include a single object to they, whoever concept feature is placed to signal around. The belongings in the document will want to look similar to this:
To instantiate the selection source inside MainActivity , override the onCreateOptionsMenu() strategy. Then phone the inflate() technique of the MenuInflater item.
Further, override the onOptionsItemSelected() solution to deal with click happenings from the diet plan items. Inside process, it is possible to call the signOut() technique of the AuthUI lessons to signal the user out. Because the sign-out procedure are executed asynchronously, we’re going to also add an OnCompleteListener to they.
Once the individual provides signed , the app should close immediately. That’s the reason why you read a phone call to your finish() means in the rule above.
5. Write A Product
Being store the chat emails for the Firebase real-time databases, you need to write a product on their behalf. The format on the chat message, which we developed earlier inside information, provides three panorama. To be able to populate those horizon, the design as well must-have about three fields.
Create a unique Java lessons called ChatMessage.java and include three representative variables to it: messageText , messageUser , and messageTime . Furthermore, include a constructor to initialize those variables.
To really make the product compatible with FirebaseUI, you should include a default constructor to they, and getters and setters for the affiliate variables.
6. Post A Chat Content
Within the listener, you need to very first become a DatabaseReference object making use of the getReference() approach to the FirebaseDatabase class. Then you’re able to contact the push() and setValue() techniques to create newer instances of the ChatMessage course into real time databases.
The ChatMessage instances must, obviously, end up being initialized with the contents of the EditText and the screen identity associated with the presently signed-in consumer.