Xamarin.Android - Working With Firebase Database - Part One

In the last article, I showed you how to add Email Authentication using Firebase in an Android app. Today, we are going to learn how to save and retrieve the data from Firebase in Android.
What is Firebase?
FireBase is a NoSQL real-time database that helps you store the data of your app on the Cloud devoid of any irritation of creating and sustaining the server. 
The prerequisites
  • Before using Firebase Real-time Database, make sure that you have connected your Android app to Firebase.
  • Android Support Library v7 AppCompat
  • Firebase.Xamarin Nuget Package
  • Android Support Design Library
  • Registered Firebase App
  • Firebase Database URL
  • Visual Studio 2017
The steps given below are required to be followed in order to create a Xamarin Firebase Database app in Xamarin.Android, using Visual Studio.
Step 1 - Create a Project
Open Visual Studio and go to New Project-> Templates-> Visual C#-> Android-> Blank app. Give it a name like FirebaseDatabase.
Step 2 - Install Android Support Library v7 AppCompat
Go to Solution Explorer-> Project Name-> References. Then, right-click "Manage NuGet Packages" and search for AppCompat. Install the AppCompat package.
Step 3 - Install Android Support Design Library
Go to Solution Explorer-> Project Name-> References. Then, right-click "Manage NuGet Packages" and search for Design Library. Install the Design Library package.
Step 4 - Install Firebase.Xamarin Nuget Package
Similarly, install the Firebase.Xamarin package.
Step 5 - Connect an Android app to FireBase
Go to firebase by using this link "https://console.firebase.google.com/u/0/", then go to the console and click on Add Project. Enter Project name and Country as shown below and then click on "Create Project".

Step 6
Now, click on ‘Add Firebase to your Android App’ and copy the package name of your app followed by a click on Register App. All other fields are optional and can be left blank for now.
Step 7
Now, download the google-services.json file and copy it to the app folder of your Android project.
If everything goes well, you will see your project added to the Firebase console.
Step 8 - Database access permissions
By default, the read and write access to your database is restricted. So, only authenticated users can read or write a data. If you haven’t added an authentication system to your app, it’s the right time to do it. You can also change the Firebase database rules that allow access to the data without requiring authentication. But it is not recommended as it makes your database publicly accessible to anyone.
For this, go to Firebase console and select your project. Now, click on Database and then select RULES tab. Replace the following code with Firebase Database Rules and hit "Publish" button.
Step 9 - Database URL 
Go to Firebase Console, select your project, and click on "Database" tab. Click on "Get Started" and copy following Database URL.
I have divided this article into two slices for the sake of simplicity. In the next part, I will be completing the remaining process of Firebase Database. 

Similar Articles