Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to integrate Firebase in Android?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 187
    Comment on it

    Hi Friends,

    This tutorial will guide to integrate Firebase in android application and store and sync data to Firebase through a very simple example.

    We will discuss complete tutorial step by step .

    Step 1- Firebase introduction

    Firebase provides a backend service which manages our apps data on the server. We can store and sync data to Firebase cloud system.  Firebase uses an API that allows our application to synchronized data on Firebase cloud. To integrate Firebase, we need to create an account on Firebase.

    Step 2- Create an App on Firebase

    First, we will login on Firebase website then we will create an application on Firebase using following URL.

    https://www.firebase.com/login/

     

    Step 3- Add supporting Library

    To use all Firebase features in our application, we need to integrate support library of Firebase in the project. So, open app's build.gradle file and add supporting library as a dependency.

    dependencies {
    compile 'com.firebase:firebase-client-android:2.5.1+'
    }

    Step 4- Add internet permission

    <uses-permission android:name="android.permission.INTERNET" />

    Step5- Setup Firebase on Android

    Firebase library must be initialized before Firebase app used and created.

    // Initialize firebase library with context
    Firebase.setAndroidContext(this);

     

    Step6 - Add Firebase DataBase URL

    To read and write data to Firebase database, we need to Firebase url.
    /**
     * Call Firebase constructor and pass database url of firebase app.
     */
    public void appReference(){
        // Replace numapp with your app name
        myFirebaseRef = new Firebase("https://numapp.firebaseio.com/");
    }
     

    Step 7- Insert data to Firebase database

    To insert value to the database, we need to call setValue() method. We can store Boolean, Long, Double,Map<String, Object> or List data types to Firebase.

     /**
         * Create Hashmap object and put birthday and full name.
         * Add this hash map in "post" key value pair
         * store data on Firebase, by using setValue() method.
         */
        public void insertData(){
    
            Firebase usersRef  = myFirebaseRef.child("tester");
    
            Map<String, String> userMap = new HashMap<String, String>();
            userMap.put("birthYear", "2012");
            userMap.put("Name", "Tester");
    
            Map<String, Map<String, String>> users = new HashMap<String, Map<String, String>>();
            users.put("Post", userMap);
            usersRef.setValue(users);
        }

     

    Step 8- Read store data from Firebase database

    We can read stored data from Firebase database by adding Listener and handling the result event. We need to call an addValueEventListener() method of child value of Firebase database.
     

    /**
         *  Adding Listener and handling the result by call addValueEventListener() method
         *  for specific value of Firebase database
         */
        public void childListener(){
    
            myFirebaseRef.child("users").addValueEventListener(new ValueEventListener() {
    
                @Override
                public void onDataChange(DataSnapshot snapshot) {
                    Log.e("result#########3", snapshot.getValue() + "....." + snapshot.getKey());
    
                    System.out.println(snapshot.getValue());
                }
    
                @Override
                public void onCancelled(FirebaseError error) {
    
                    Log.e("The read failed: ", error.getMessage());
                }
    
            });
        }

    Step 9- Store multiple json data

    We can store multiple json data to Firebase database.

    /**
     * Created a "MultiUser" child.
     * Created separate multiple object.
     * Adding all object into a new HaspMap and pass it into setValue() method
     */
     public void multiChild(){
     
         Firebase usersRef  = myFirebaseRef.child("MultiUser");
     
         Map<String, Object> name1 = new HashMap<String, Object>();
         name1.put("name", "Tester1");
     
         Map<String, Object> name2 = new HashMap<String, Object>();
         name2.put("name", "Tester2");
     
         Map<String, Object> nickname = new HashMap<String, Object>();
     
         nickname.put("abc", name1);
         nickname.put("xyz", name2);
     
         usersRef.setValue(nickname);
     }
    
    
    

    Step 10- Updating specific field

    If we have need to update any specific field in Firebase table then we have to call  updateChildren() method instead of setValue() method.

     

    /**
     * Update MultiUser child node data
     */
    public void updateData(){
     
        Firebase usersRef = myFirebaseRef.child("MultiUser");
     
        Firebase alanRef = usersRef.child("Tester1");
        Map<String, Object> nickname = new HashMap<String, Object>();
        nickname.put("name", "Android Developer");
        alanRef.updateChildren(nickname);
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: