Firebase SDK integration and Phone Number Verification
Before starting this blog one thing comes in my mind what is, why, when, and how should I use Firebase. Let’s see it one by one-
- Firebase is a platform facilitates web and mobile to develop a high-quality application. Acquired by Google in 2014 from Firebase, Inc.
- When we design a web app or mobile app database is a big problem. It uses a lot of bandwidth for a large amount of data, we have to save in DB. So, it’s not easy to manage all these things. Thus, firebase is a solution for that.
- Manages the problem of Database in real-time. Solve the scaling problem.
- Firebase provides us all sorts of authentication such as one-time click authentication via Google, Facebook, Apple, Twitter, etc. or phone number verification.
- It does cloud messaging, app notification, advertisement programs.
- Also, provide Firebase Analytics components.
How to Configure the Firebase SDK in Android:
Below are the steps to configure firebase:
1. Open Firebase console:
Add project-
a) Write the project name
b) Configure Google Analytics if required
Now, the project has been created.
2. Project Overview Page to register app:
a.) Click on the Android icon to register App
b.) Enter your package name, SHA1 (optional) and App NickName (optional) to register app
3. Now, download App Config File i.e. google-service.json
a.) Move your app-config file to your app
b.) To copy it in our app first goto Project view then click MyApp -> app and paste it. As shown below:
4. To enable Firebase products google-service to add plugins in your Gradle:
a.) Add
apply plugin: ‘com.google.gms.google-services’
in build.gradle(Module:app) i.e. app-level gradle file.
b.) Also, add below dependency
classpath ‘com.google.gms:google-services:<latest-version>’
in build.gradle(Project:YourApp) i.e. project level gradle file.
5. If you have enabled Google Analytics then to add SDK in your project add below the line as a dependency
implementation ‘com.google.firebase:firebase-analytics:<latest-version>’
in build.gradle(Module:app) i.e. app-level gradle file.
Now, run the app to check whether your app is connected with Firebase or not (work only in a physical device, not emulator)
For SHA in Android Studio:-
Click Gradle setting on the right side corner
Click on YourApp-> Tasks -> android -> signing report
Will get all the data related Config: debug
Now, Let's see how to configure firebase Automatically, it’s pretty simple and easy to configure:
To configure firebase Automatically in your App:
- Open your app in Android Studio.
- Select Tools > Firebase to open the Assistant pane
- Click on the tools you want to use let’s say we clicked on Analytics -> login
- After login, it will connect your app to firebase
- Click Add Analytics to your app.
- Follow all the steps as the tutorial guides you.
- If you added Analytics, run your app to send verification to Firebase that you’ve successfully integrated Firebase. Otherwise, you can skip this verification step.
As shown below:
Authenticate a Phone Number using Firebase on Android
Here is the app link for phone number authentication, code explanation detail is in the app: PhoneNumberVerificationApp
Firebase made Phone numbers authentication very easy.
Note: Here, are three points which you have to follow before running the app
1. Before running the sample app change App Config File i.e. google-service.json with your config file.
2. To do authentication it is necessary to add SHA1 in your app. If you haven’t added it before.
3. Firebase enforces a limit on the number of SMS messages that can be sent to a single phone number within a period of time. If you exceed this limit, phone number verification requests might be throttled. If you encounter this issue during development, use a different phone number for testing, or try the request again later.
Hope, it helps every one to get some knowledge of firebase. In the future, I will write more about firebase storage.
Thank you,
Happy Learning.