It might look a bit scary at first. But don’t panic and let’s check how to do this right step by step.
Firebase account and project registration
Follow these steps to register your Firebase account and create a Firebase project:
Register a Firebase account at Firebase console .
You can use your Google account to authenticate at Firebase.
Click Create project
!> Note: If you have a Google project registered for your mobile app, select it from the Project name dropdown menu.
You can also edit your Project ID if you need. A unique ID is assigned automatically to each project. This ID is used in publicly visible Firebase features. For example, it will be used in database URLs and in your Firebase Hosting subdomain. If you need to use a specific subdomain, you can change it.
Fill in the fields required (Project name, Project ID, etc.) and click Continue.
Configure Google Analytics for your project and click Create project.
Then click Continue.
Select platform for which you need Firebase
Fill in the fields on Add Firebase to your Android App screen and click Register App
Connect Firebase SDK
For Android projects Firebase has the following requirements to be added successfully:
Android OS 4.0 or newer
Google Play Services 15.0.0 or higher
The latest version of Android Studio
Here is a step by step guide how to connect Firebase SDK to your Android Project:
Download Google-Services.json config file
Open Project view in Android Studio and upload Google-Services.json file you have just downloaded into the root directory of your Android app module.
Add Firebase SDK according to the instructions in your Firebase console
Add Google services plugin to your project build.gradle file
Add Google services plugin in the bottom of your module build.gradle file.
Click Sync Now at the pop-up in your Android Studio
You are done now.
Firebase authentication
This option allows users in your app authenticate themselves via phone number. If you use this method for user authentication, the user receives an SMS with verification code and authenticates via that code in your app.
You need to follow these steps to add Firebase authentication to your Android project:
Add Firebase authentication dependency to your Module build.gradle file:
Add your app’s SHA-1 hash in your Firebase console >> Project settings tab:
And then:
Go to Firebase console >> Authentication >> Sign-in method section:
Enable Phone number sign-in method:
Add PhoneAuthProvider.verifyPhoneNumber method to request that Firebase verify the user’s phone number:
!> Note: verifyPhoneNumber method is reentrant: if you call it multiple times, such as in an activity’s onStart method, verifyPhoneNumber method will not send a second SMS unless the original request has timed out.
!> Important note: As a best practice please do not forget to inform your users that if they use phone sign-in, they might receive an SMS message for verification and standard rates apply.
(Optional) To resume the phone number sign in process if your app closes before the user can sign in (if the user checks SMS app, for instance), after verifyPhoneNumber method is called, set a flag that indicates verification is in progress. Then, save the flag in your Activity’s onSaveInstanceState method and restore the flag in onRestoreInstanceState. In your Activity’s onStart method, check if verification is already in progress, and if it is not, call verifyPhoneNumber again. Be sure to clear the flag when verification completes or fails. Check this guide for more details.
setLanguageCode method on your Auth instance allows specifying the auth language and therefore localize SMS message sent by Firebase:
When you call PhoneAuthProvider.verifyPhoneNumber method, you must also provide an instance of OnVerificationStateChangedCallbacks, which contains implementations of the callback functions that handle the results of the request:
Create a PhoneAuthCredential object using the verification code and the verification ID that was passed to onCodeSent callback. You get a PhoneAuthCredential object when onVerificationCompleted is called.
To create PhoneAuthCredential object, call PhoneAuthProvider.getCredential:
Complete the sign-in flow by passing the PhoneAuthCredential object to FirebaseAuth.signInWithCredential:
Get Firebase access_token after SMS code verification as follows:
Example of the method implementation:
Add ConnectyCube user sign in to your project as follows:
Get your Project ID from your Firebase console:
Pass your Firebase project_id and Firebase access_token parameters to signInUsingFirebase method: