Firebase Database Example in Android

What is Firebase :

                 The Firebase Realtime Database is a cloud-hosted NoSQL database that lets you store and sync data between your users in realtime.The Firebase Realtime Database is a cloud-hosted database. Data is stored as JSON and synchronized in realtime to every connected client. When you build cross-platform apps with our iOS, Android, and JavaScript SDKs, all of your clients share one Realtime Database instance and automatically receive updates with the newest data.

NEW: Cloud Firestore (beta) enables to you store, sync and query app data at global scale.



Setup Firebase Realtime Database for Android :

 There are two ways you can connect your app to Firebase .

    1.Install Firebase SDK
    2.Using Firebase Console,add your app to Firebase .

 Install Firebase SDK :

If you're using the latest version of Android Studio (version 2.2 or later), we recommend using the Firebase Assistant to connect your app to Firebase. The Firebase Assistant can connect your existing project or create a new one for you and automatically install any necessary gradle dependencies.

If you're using an older version of Android Studio or have a more complex project configuration, you can still manually add Firebase to your app.

Firebase Assistant :

To open the Firebase Assistant in Android Studio:

    1.Click Tools > Firebase to open the Assistant window.
    2.Click to expand one of the listed features (for example, Analytics), then click the provided tutorial link (for example, Log an Analytics event).
    3.Click the Connect to Firebase button to connect to Firebase and add the necessary code to your app.

That's it!

Manually Add Firebase :

If you prefer not to use the Firebase Assistant, you can still add Firebase to your app using the Firebase console.

To add Firebase to your app you'll need a Firebase project and a Firebase configuration file for your app.

    1.Create a Firebase project in the Firebase console, if you don't already have one. If you already have an existing Google project associated with your mobile app, click Import Google Project. Otherwise, click Add project.
   2.Click Add Firebase to your Android app and follow the setup steps. If you're importing an existing Google project, this may happen automatically and you can just download the config file.
    3.When prompted, enter your app's package name. It's important to enter the package name your app is using; this can only be set when you add an app to your Firebase project.
    4.At the end, you'll download a google-services.json file. You can download this file again at any time.
    5.If you haven't done so already, copy this into your project's module folder, typically app/.

Add the SDK :

If you would like to integrate the Firebase libraries into one of your own projects, you need to perform a few basic tasks to prepare your Android Studio project. You may have already done this as part of adding Firebase to your app.

First, add rules to your root-level build.gradle file, to include the google-services plugin and the Google's Maven repository:


buildscript {
    // ...
    dependencies {
        // ...
        classpath 'com.google.gms:google-services:3.1.1'
    }
}

allprojects {
    // ...
    repositories {
        // ...
        maven {
            url "https://maven.google.com" // Google's Maven repository
        }
    }
}


 Then, in your module Gradle file (usually the app/build.gradle), add the apply plugin line at the bottom of the file to enable the Gradle plugin:


apply plugin: 'com.android.application'

android {
  // ...
}

dependencies {
  // ...
  compile 'com.google.firebase:firebase-core:11.6.0'
 
}
// add this at the bottom
apply plugin: 'com.google.gms.google-services'

 That's It.

 Add Realtime Database in App
                                                      Add the dependency for Firebase Realtime Database to your app-level build.gradle file:

compile 'com.google.firebase:firebase-database:11.6.0'

 Write to your Database :
                                      First you need to retrieve your database instance using getInstance() method. Like this

FirebaseDatabase database = FirebaseDatabase.getInstance();

 Then you have to point your location in database using DatabaseReference
class . Like this

DatabaseReference myRef = database.getReference("message");

 Then push or set your message or text in that particular ("message") location using setValue() method . Like this

myRef.setValue("Hello, World!");

Read from your Database :
                                              In that case to make your app data update in realtime , you should add a ValueEventListener to the reference you just created. Like this

// Read from the database
myRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // This method is called once with the initial value and again
        // whenever data at this location is updated.
        String value = dataSnapshot.getValue(String.class);
        Log.d(TAG, "Value is: " + value);
    }

    @Override
    public void onCancelled(DatabaseError error) {
        // Failed to read value
        Log.w(TAG, "Failed to read value.", error.toException());
    }
});


Find Us :

        Facebook  :@apnaandroid
        Google+   : Apna Java
        Youtube   : Android & Java Tutorial

Comments

Popular posts from this blog

Disable/Hide Year from DatePickerDialog in android

Coordinator Layout in Android

Custom Calendar in android