How to save data to Firebase in Android?

Objectives:

The purpose of this blog is to inform readers on how to save data in Android’s Firebase Realtime database. Simple data kinds, such as strings, can be written to this database, as well as more complicated data types, like Java objects.

Pre-Requisites:

To use the Firebase Realtime Database in Android, we need to have an Android Studio project for creating the android app. Besides that, we need a Firebase project to connect the Firebase real-time database to the Android app.

To create a new Firebase project, follow the below-mentioned steps:-  

1. Open the URL – https://console.firebase.google.com

2. Then click on “Add Project”.

3. Next, enter the Firebase Project name and accept the conditions. After that, proceed by clicking on the “Continue” button.

4. Then, on the next Welcome screen, click on “Add Firebase to your Android app“. In the same manner, enter the package name and click on Next.

5. Now, download the google-services.json file to your local system. Then, copy this file to the location in the Android Studio project as specified.

6. Follow the small process in the next step to add 3 lines of code to the build. Gradle files at project-level and app-level.

7. Then, finish adding the Android app to the Firebase project.

8. Open the Database section by clicking on the “Database” option in the left menu. Then, click on the “Create Database” link.

9. Now the process of creating the Firebase project is finished.

Recommended Read: How to fetch data from Firebase in Android?

Add the following line in the app-level build. gradle file to add the library to start using the Firebase Realtime Database.

implementation 'com.google.firebase:firebase-database:16.0.6'

Code Implementation

Before proceeding further, let us understand what is a Firebase reference?

In the Firebase database, the data is stored at a location. Hence, the pointer to that location is known as a Firebase reference. Multiple references point to the same location if they are initiated with the same path.

Create an instance of Firebase Database by writing the following line of code:

private FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();

Now create references of the paths where the data would be stored in the Realtime Database. As the data is stored in the JSON format in the Firebase database, the paths specified in the references are the keys of the JSON object.

Recommended Read: How to fetch data from Firebase in Swift Language (iOS)?

The following line of code has to be added to create a reference to the root node in the database:

private DatabaseReference mDatabaseReference = mDatabase.getReference();

Next, create a child under the root node in the database and use this child key to add the corresponding data. After that, a string can be added to the database by following the below-mentioned lines of code:

mDatabaseReference = mDatabase.getReference().child("name");
mDatabaseReference.setValue("Donald Duck");

The above lines of code would insert the data into the database. The database in the Firebase project will look as follows:

Firebase Realtime database also supports saving complex Java objects. Hence, a model class needs to be created first for saving the Java objects. Also, make sure to add an empty constructor and each attribute’s getters in the class.

Following is the code for a demo model class:

Following is the code to save an object of this class to the Firebase database:

User user = new User("Mickey", 18);
mDatabaseReference = mDatabase.getReference().child("user");
mDatabaseReference.setValue(user);

The above-mentioned code would insert the object data in the  Firebase database as follows:

Recommended Read: How to save data to Firebase in Swift Language (iOS)?

Conclusion:

Now you are knowing how to save data into the Firebase Realtime Database. Also, Knowband offers an eCommerce Mobile App Builder module for online Store owners. So, the mobile app Maker plugin is available for platforms like PrestaShop, OpenCart, WooCommerce and Magento 2. Thus, With just a few clicks and no difficulty, you can create your native, responsive app for smartphones and tablets.

Steps to use Google Authenticator App for 2 Factor Authentication for GoDaddy

AA free security programme called Google Authenticator helps guard your accounts against password theft. Two-factor authentication, which is available on popular sites including Gmail, Facebook, Twitter, GoDaddy and Instagram, is simple to set up and use.

The app (iOS/Android) generates a random code that is used to verify your identity when you log into various services. The Google Authenticator app offers an additional layer of security over simply sending the code to your phone through text message each time.

Step by Step Guide to Use Google Authenticator app for 2 Factor Authentication for Godaddy:

Install the 2-step Google Authenticator App for increased account security.

Recommended Read: How to download your Android/iOS app from the Diawi link?

1. Install Google Authenticator App: https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator

2. Click on the + icon at the right bottom of the app screen. This would give you an option to scan a QR Code.


3. Log in to the Godaddy account of MorroCapital and goto: https://account.godaddy.com/security/

Recommended Read: iOS & PWA: Key Points Everyone Should Know

4. On the right bottom part you will see an option to ADD BACKUP. Click it.


5. It will take you to the next screen where you need to select “Authenticator App” as the option and click NEXT.

6. On the new screen it would show a QR code to you. You need to Scan that QR code with your Google Authenticator app (Step:2)

7. Then Click NEXT and it would ask you to fill in the code. Simply enter the code that is displayed in the Google Authenticator app and click Continue. Additionally, if the code is accurate, the app will be set up and you won’t need to rely just on SMS anymore. Although you can always use SMS as the authentication option while logging into the account.

Recommended Read: 3 Robust Apps Created using KnowBand’s Mobile App Builder Plugin for Poland PrestaShop Stores

In The End:

Passwords may be easy to guess or obtain in other ways. It is uncommon for the hacker to additionally have access to the user’s actual device because the bulk of attacks takes place online.

Knowband, an eCommerce plugin development company, offers the most sophisticated and automated way to expand your online business to mobile platforms. Using the Mobile App Builder, they can turn your complete website into a mobile app with features. Also, it enables the store admin to sell things on mobile devices in addition to the standard website. The app’s features and operation perfectly mirror those of the website and operate on both Android and iPhone devices. To know in detail about the eCommerce mobile app builder, reach out to us at support@Knowband.com