Kickstarter for Firebase Database on Android

http://justmobiledev.com/wp-content/uploads/2017/12/firebase-database-android-title.pnghttp://justmobiledev.com/wp-content/uploads/2017/12/firebase-database-android-title.pnghttp://justmobiledev.com/wp-content/uploads/2017/12/firebase-database-android-title.pnghttp://justmobiledev.com/wp-content/uploads/2017/12/firebase-database-android-title.pngKickstarter for Firebase Database on Android

Here a quick and dirty tutorial on how to get started with Firebase Realtime Databases on Android.

Firebase Database Concepts

First, let’s review a couple of concepts before we get started:

Database Types

There are two types of databases available: Realtime Database and Firestore, the newer beta version with improved performance and scalability.
Realtime Database
This is Firebase’s original database. It’s an efficient, low-latency solution for mobile apps that require synced states across clients in realtime

Firestore Database
Cloud Firestore is Firebase’s new flagship database for mobile app development. It improves on the successes of the Realtime Database with a new, more intuitive data model. Cloud Firestore also features richer, faster queries and scales better than the Realtime Database.

For a detailed comparison of the two database types, please have a look at the Firebase guide.

Data Structures
All objects are stored in JSON format and in order to store them, it is not necessary to create a table structure first.
In order to identify your data, you can use your own custom ids (e.g. UserId for a User), or you can use the ids provided to you in the .push() method.

Firebase suggests that you avoid nested JSON structures, since it will affect performance and you may hit the nesting limit of 32 levels. Try to stick with simple, flat JSON structures.

Android Setup

First, create a Single Page Project in Android. Alternatively, you can download the Starter Project from my GitHub site and follow along.

Android Permissions
First you want to add a permission for the app to access the internet. This is done in the AndroidManifest.xml. The permissions node is a child of ‘manifest’ (not application).

1
2
3
4
5
6
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.mobile.justmobiledev.firebasedatabasestarter" >
    <!-- PERMISSIONS -->
    <uses-permission android:name="android.permission.INTERNET" />
    <application>
...

Adding Dependencies

In your project gradle file (not application gradle), add the dependency for the google services plugin:

1
2
3
4
5
6
7
8
9
buildscript {
    repositories {
        ...
    }
    dependencies {
        ...
        classpath 'com.google.gms:google-services:3.1.1' // google-services plugin
    }
}

In the same file, add the Google Maven repository in the repositories section:

1
2
3
4
5
6
7
8
allprojects {
    repositories {
        ...
        maven {
            url "https://maven.google.com" // Google's Maven repository
        }
    }
}

First, add the dependency for firebase cloud storage to your application gradle file:

1
2
3
4
5
dependencies {
    ...
    compile 'com.google.firebase:firebase-database:11.8.0'
    ...
}

At the end of your module build grade file, add the statement to apply the Google plugin:

1
2
// At the bottom of the file
apply plugin: 'com.google.gms.google-services'

Go ahead and sync your gradle (Tools / Android / Sync Project With Gradle Files).

Firebase Setup

If you haven’t already, register for a Firebase account. As of Feb 2016, you are required to use a Google gmail account to log in to Firebase, so if you haven’t already, go ahead and get a Gmail account.

Create a new Firebase Project and call it ‘FirebaseDatabaseStarter’ or select your own name.

Let’s go ahead and create an Android application for your Firebase Project. On the left navigation bar, select the gear icon an then ‘Project Settings’.

Next, go ahead and click on the ‘Android’ button (Add Firebase to your Android appp). On the Android app pop-up, fill out the app package name and a nick name.

NOTE: You want to make sure that your package name you enter in Firebase matches your actual Android project package name exactly. If you are not sure what your project name is, check in your Android project (e.g. under app/java).

In the next step, you can download your google-services.json, and add it to your Android project. This file contains your Firebase API Key and information about your Firebase setup, such as your Firebase storage URL.

So download the json file, open Android Studio, switch to Project View an drag & drop the google-services.json under the app directory (your project root).

Setting Up Database Access Rules
The Firebase Database functionality provides a rule language to control access to the database entities. By default, only authorized users can access the database. For the purpose of this tutorial, let’s open the access, so that unauthorized users can also access the database.

For more information about Firebase Database rules, please refer to the Firebase guide.

In the Firebase Console, click on ‘Develop’ on the left navigation bar, select ‘Database’ and then switch to the rules tab:

1
2
3
4
5
6
{
  "rules": {
    ".read": true,
    ".write": true
  }
}

This means, even unauthorized users have read/write access.

Writing Data

First let’s go ahead and store an object in our database. Firebase offers a convenient way to directly store Java objects in the cloud.
The following basic data types are currently supported:

  • String
  • Long
  • Double
  • Boolean
  • Map
  • List

    So let’s go ahead and create a simple Java class called ‘Note’, which we want to store in the cloud. Add a new Java class file to your Android project:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    @IgnoreExtraProperties
    public class Note {

        public String title;
        public String body;

        public Note() {
            // Default constructor required for calls to DataSnapshot.getValue(User.class)
        }
        public Note(String title, String body) {
            this.title = title;
            this.body = body;
        }
    }

    Next, create a new method in your MainActivity.java to create a note. The method contains the following steps:
    1. Create a reference to the Firebase Database singleton
    2. Add a reference to a folder in your main database storage for all notes
    3. Create a new unique key for this Note
    4. Create a reference to the new Note in the cloud
    5. Create a note object with data
    6. Save the note to cloud storage using the ‘new note’ reference and add a completion handler
    7. Check that the note was successfully stored by using the databaseError variable in the completion handler.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
        private void writeNoteToCloud()
        {
            try {
                // Create Reference
                DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference(); // 1

                // Add a child node for all notes
                DatabaseReference notesRef = dbRef.child("notes"); // 2

                // Get a new key for the Note
                String newNoteId = notesRef.push().getKey(); // 3
                Log.d(TAG, "NoteId: "+newNoteId);

                // Create a reference to the new note
                DatabaseReference noteRef = notesRef.child(newNoteId); // 4

                // Create a note
                Note note = new Note(newNoteId, "My first note", "This is my Note body"); // 5

                // Create the note async with completion handler
                noteRef.setValue(note, new DatabaseReference.CompletionListener() { // 6
                    @Override
                    public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) { // 7
                        if (databaseError != null) {
                            Log.e(TAG, "Note could not be saved " + databaseError.getMessage());
                        } else {
                            Log.d(TAG, "Note saved successfully.");
                        }
                    }
                });
            }
            catch(Exception ex)
            {
                Log.e(TAG, ex.getMessage());
            }
        }

    Once you run the application an refresh the Firebase Console, you should now be able to see your first note:

    Querying for Data

    There are several ways to query for data. A way that differs from the conventional relational database approach is to register a listener to any changes to a collection of object and then evaluation the result in the event handler.

    Firebase database also offers the Query class, which has a number of methods for querying, setting limits, and ordering the results. A complete list of methods can be found in the Firebase documentation.

    Below is a simple example, how to query for the Note we created using the primary key that we generated. Here are the steps:

    1. Get a reference to Firebase Storage singleon
    2. Get a reference to the actual file path
    3. Create a query object with the .equalTo() method and pass the primary key
    4. Register listeners for the SingleValueEvent
    5. Get the first value in the result set and cast it to the ‘Note’ object.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
        private void queryNoteFromCloud()
        {
            try {
                // Create Reference
                DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference(); // 1

                // Add a child node for all notes
                DatabaseReference notesRef = dbRef.child("notes"); // 2

                Query query = notesRef.equalTo(newNoteId); // 3
                query.addListenerForSingleValueEvent(new ValueEventListener() { // 4
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.exists() && dataSnapshot.hasChildren()) {
                            // Get the first match
                            DataSnapshot noteSnapshot =  dataSnapshot.getChildren().iterator().next(); // 5
                            Note note = noteSnapshot.getValue(Note.class);
                            Log.d(TAG, "Note: " + note.title + ", body: " + note.body);
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }
            catch(Exception ex)
            {
                Log.e(TAG, ex.getMessage());
            }
        }

    Updating Data

    Firebase Database also provides convenient ways to update object attributes or whole objects in the cloud by using the object references:

    1. Get the FirebaseDatabase singleton
    2. Get a reference to the notes node
    3. Update the Note title by using the Note object’s unique id
    4. Alternatively, replace the whole Note object

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
        private void updateNote()
        {
            try {
                // Create Reference
                DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference(); // 1

                // Add a child node for all notes
                DatabaseReference notesRef = dbRef.child("notes"); // 2

                // Update title attribute
                notesRef.child(newNoteId).child("title").setValue("this is the new title"); // 3

                Note note = new Note(newNoteId, "new title", "new body"); // 4
                notesRef.child(newNoteId).setValue(note);
            }
            catch(Exception ex)
            {
                Log.e(TAG, ex.getMessage());
            }
        }

    Removing Data

    You can use the .removeValue() method to remove an object from the database:

    1. Get the FirebaseDatabase singleton
    2. Get a reference to the notes node
    3. Get a reference to the Note object by using the unique key and use the .remove() method to remove the value. Add a CompletionListener
    4. Evaluate the result of the CompletionListener

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
        private void removeNote()
        {
            try {
                // Create Reference
                DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference(); // 1

                // Add a child node for all notes
                DatabaseReference notesRef = dbRef.child("notes"); // 2

                // Update title attribute
                notesRef.child(newNoteId).removeValue(new DatabaseReference.CompletionListener() { // 3
                    @Override
                    public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) { // 4
                        if (databaseError != null) {
                            Log.e(TAG, "Note could not be deleted " + databaseError.getMessage());
                        } else {
                            Log.d(TAG, "Note deleted successfully.");
                        }
                    }
                });
            }
            catch(Exception ex)
            {
                Log.e(TAG, ex.getMessage());
            }
        }

    That’s all I wanted to show in this FirebaseStorage starter tutorial. I hope it helps you get up to speed.

    Cheers,

    Chris

    Author Description

    justmobiledev

    No comments yet.

    Join the Conversation