As soon as the software for Android os speak initiate, it needs to check if the user try finalized in

Avatar for adminby admin
February 17, 2022

As soon as the software for Android os speak initiate, it needs to check if the user try finalized in

Step 1: Handle User Sign-In

In that case, the application should go forward and exhibit the contents of the chat room. Otherwise, it ought to redirect the user to either a sign-in screen or a sign-up display . With FirebaseUI, promoting those screens takes way less code than you possibly might think about.

Within the onCreate() way of MainActivity , find out if an individual is signed in. To achieve this, verify that current FirebaseUser object isn’t null . When it’s null , you need to make and configure an Intent object that starts a sign-in task. To do so, make use of the SignInIntentBuilder course. When the purpose is ready, you have to introduce the sign-in activity by using the startActivityForResult() means.

Keep in mind that the sign-in task additionally allows new users to register. Therefore, you don’t need to create any further rule to address consumer registration.

As you can plainly see during the preceding rule, if the consumer is finalized in, we 1st highlight a Toast welcoming an individual, after which contact a technique named displayChatMessages. For now, only make a stub because of it. We’ll end up being incorporating laws to it afterwards.

Once the user keeps signed in, MainActivity will get a consequence in the form of an intention . To address they, it is vital that you bypass the onActivityResult() method.

In the event that result’s laws are RESULT_OK , this means the consumer has signed in successfully. In that case, you need to call the displayChatMessages() system once again. Usually, call finish() to shut the application.

2: Manage Consumer Sign-Out

Automatically, FirebaseUI utilizes wise Lock for Passwords. For that reason, after the consumers sign in, they will stay closed in even if the app was restarted. To permit the customers to sign around, we will today create a sign-out option to the overflow selection of MainActivity .

Generate a new menu source document known as main_menu.xml and incorporate a single product to it, whoever subject feature is placed to signal on. The contents of the document will want to look along these lines:

To instantiate the selection reference inside MainActivity , override the onCreateOptionsMenu() strategy. Next phone the inflate() method of the MenuInflater item.

Further, override the onOptionsItemSelected() solution to handle click occasions on the menu product. Inside the method, possible name the signOut() technique of the AuthUI lessons to signal the user completely. Because the sign-out process is actually performed asynchronously, we will include an OnCompleteListener to they.

Once the consumer enjoys signed out, the app should nearby automatically. For this reason the reason why you discover a call for the finish() approach during the rule over.

5. Create A Product

To be able to put the chat information in Firebase real time database, you should generate a product for them. The layout associated with chat content, which we created earlier in this tutorial, enjoys three opinions. To be able to populate those opinions, the product as well must have no less than three sphere.

Generate a brand new coffee course called and incorporate three representative variables to it: messageText , messageUser , and messageTime . Also, add a constructor to initialize those factors.

To really make the unit suitable for FirebaseUI, it is vital that you also add a default constructor to it, combined with getters and setters for all your user variables.

6. Post A Chat Information

In the listener, you need to initial get a DatabaseReference object utilising the getReference() method of the FirebaseDatabase class. You’ll be able to name the push() and setValue() strategies to put latest instances of the ChatMessage lessons towards real time database.

The ChatMessage instances must, obviously, getting initialized utilising the belongings in the EditText together with screen identity in the presently signed-in consumer.

Avatar for admin

Leave a comment