Firebase MAD
Firebase MAD
1 Introduction
Today we’ll be going over Firebase which is a database backend. It provides various features such
as authentication and cloud data storage. We will build off a basic app and add firebase to it.
This is a very simple app that just has info on a class with students with names and ids.
Click Here to download the basic app that we’ll be adding Firebase to.
Also, Click Here to download the completed firebase app. If at any time you are stuck at a part,
you can refer to the completed app for help.
2 Firebase
Now go to https://www.firebase.com/login/ and login a google account and create a project. Keep
track of your project ID. As you can see in the screenshot, mine is himitsuno-fecb4
We will now change our project’s authentication rules to public. On the left, click on Database >
Rules. You should be on this page. Here you can set who is allowed access to your database.
1
3 Adding Firebase to Basic App as a dependency
Now that we have finished setting up Firebase, open the basic app in Android Studio. If it tells you
to update or install build tools, say yes. First we will add Firebase as a project dependency. Now
navigate to BasicApp > app > build.gradle. Scroll to the bottom and you’ll see Dependencies.
Add to the bottom of dependencies this line
compile ’com.firebase:firebase-client-android:2.5.0+’
Also add some excludes to our Android packaging options
android {
packagingOptions {
exclude ’META-INF/LICENSE’
exclude ’META-INF/LICENSE-FIREBASE.txt’
exclude ’META-INF/NOTICE’
}
}
Your build.gradle file should now look like Click Here. Click sync. It is the circular arrows button
next to save.
2
Now we create a reference to the Firebase database using the Firebase Project ID we saved earlier.
under public void submit we initialize the firebase constructor with your firebase url.
Firebase myFirebaseRef = new Firebase("https://<firebase-url>.firebaseio.com");
For my Firebase this would be:
Firebase myFirebaseRef = new Firebase("https://himitsuno-fecb4.firebaseio.com");
With this reference we can now write any data value to it. For example, to write the student’s Id
it will just be:
myFirebaseRef.child(student.getStudentId()).setValue(student);
As a reminder this is a very simple app that just has info on a class with students with names
and ids.
This is a good time to run our app to make sure nothing is wrong. By this time we should have
firebase connected and the ability to connect studentID to studentName. If we all did it correctly
it will sync and run with no problems. Click on Add Record and go ahead and add a student to
the database. In my example I added Himitsuno with student ID 0009. When you click on submit
this entry will be written to the Firebase database.
Your MainActivity file should now look like Click Here with your Firebase url instead of mine.
3
import com.firebase.client.Query;
import com.firebase.client.ValueEventListener;
First initialize the reference with your Firebase ID like we did in MainActivity.
Firebase myFirebaseRef = new Firebase("https://<firebase-url>.firebaseio.com");
For my Firebase this would be:
Firebase myFirebaseRef = new Firebase("https://himitsuno-fecb4.firebaseio.com");
Remember to use your own Firebase ID! Now add this huge chunk of code at the bottom of public
void search
Firebase myFirebaseRef = new Firebase("https://himitsuno-fecb4.firebaseio.com");
Query queryRef = myFirebaseRef.orderByChild("studentId").equalTo(idToSearch);
queryRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot == null || snapshot.getValue() == null)
Toast.makeText(SearchActivity.this, "No record found", Toast.LENGTH_SHORT).show();
else
Toast.makeText(SearchActivity.this, snapshot.getValue().toString(),
Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(FirebaseError error) {
}
});
Essentially we attach an event listener, write a query reference, search the database once for our
query and then unregister the listener. In this case it is useful for a callback to be called once and
then immediately removed. That is exactly what addListenerForSingleValueEvent does.
Your SearchActivity file should now look like Click Here with your Firebase url instead of mine.
Now sync and run BasicApp again and we can now search for students in our database by ID.
For example, if I input ID 0009 and hit search it will return the student Himitsuno I added earlier.
4
Once you have made it this far, congragulations. You have completed the Firebase tutorial. If
you are stuck on any point please refer to the completed Firebase app and see where you went
wrong. If there are any further questions you are welcome to come to our Office Hours!