Step One: Handle User Sign-In
If so, the application is going ahead and show the items in the talk space. Or else, it ought to redirect the user to either a sign-in display screen or a sign-up display screen. With FirebaseUI, creating those screens takes a lot less code than you might picture.
In the onCreate() way of MainActivity , verify that the user is signed in. To achieve this, find out if current FirebaseUser object isn’t null . When it’s null , you should develop and arrange an Intent item that opens a sign-in task. To do this, utilize the SignInIntentBuilder lessons. After the purpose is ready, you have to start the sign-in task with the startActivityForResult() strategy.
Note that the sign-in activity furthermore enables new users to join up. colombiancupid Thus, you don’t need to create any additional code to undertake user subscription.
As you can see inside above rule, if individual has already been closed in, we first highlight a Toast welcoming an individual, right after which phone a way known as displayChatMessages. For now, simply make a stub for it. We are going to be incorporating signal to it afterwards.
As soon as individual features closed in, MainActivity will receive a consequence in the shape of a purpose . To deal with they, you must bypass the onActivityResult() means.
When the result’s laws was RESULT_OK , it indicates the user enjoys finalized in effectively. If so, it is vital that you call the displayChatMessages() system once again. If not, contact finish() to close the software.
2: Handle User Sign-Out
Automagically, FirebaseUI utilizes Smart Lock for Passwords. Thus, as soon as consumers register, they will stay finalized in even when the application are restarted. Allowing the users to signal out, we’re going to now put a sign-out substitute for the overflow selection of MainActivity .
Build a diet plan site file also known as main_menu.xml and put one item to it, whoever subject feature is defined to indication aside. The belongings in the document will want to look similar to this:
To instantiate the diet plan reference inside MainActivity , override the onCreateOptionsMenu() method. After that contact the inflate() technique of the MenuInflater item.
Further, override the onOptionsItemSelected() method to deal with click activities regarding the eating plan item. Within the means, you can name the signOut() technique of the AuthUI lessons to signal the consumer on. Because sign-out process is executed asynchronously, we are going to also add an OnCompleteListener to they.
Once the consumer has actually signed out, the application should close immediately. This is why the reasons why you discover a phone call into the finish() strategy from inside the code over.
5. Develop An Unit
Being keep the chat emails in the Firebase real-time databases, it is vital that you build a product on their behalf. The format on the chat information, which we developed previously inside tutorial, enjoys three vista. To populate those views, the design too should have at the least three sphere.
Build a fresh Java lessons also known as ChatMessage.java and put three representative factors to it: messageText , messageUser , and messageTime . Also, include a constructor to initialize those variables.
To really make the model compatible with FirebaseUI, it is vital that you include a default constructor to they, alongside getters and setters for the user factors.
6. Blog Post A Cam Content
Inside the listener, you need to initially become a DatabaseReference object utilising the getReference() technique of the FirebaseDatabase course. You’ll be able to name the push() and setValue() ways to put newer instances of the ChatMessage course to your real-time database.
The ChatMessage instances must, however, become initialized utilising the belongings in the EditText plus the screen identity on the presently signed-in individual.