Android access to Firebase Realtime Database using Anonymous Authentication

Introduction

In this post, we are going to learn how to access firebase real-time database using anonymous authentication. We will cover accessing default and non-default database in this post.

Prerequisites

  • Google Firebase Project
  • Android App development environment

Note: This post would be useful for those who are likely to integrate firebase real-time database into their Android App.

Firebase project settings

In this section, we will cover all the steps needs to be done under Google Firebase project.

Enable Firebase Anonymous Authentication

Go to ‘SIGN-IN METHOD’ section under Authentication service of your firebase project. Enable Anonymous provider under sign-in providers.
firebase enable anonymous authentication

Add Android app to Firebase Project

Create a new Android app and add to firebase project.
firebase android add app

Add root android app package.
firebase android add package

google-services.json download and configuration

Download google-services.json from Project Settings -> General section.
project setttings google services json

Paste google-services.json into app sub-directory under your Android app project.

Firebase Realtime Database Setup

Setup a new realtime database in your project (Skip this step if already done before). Add a new child user -> user1 in the database. Under user1 and attributes fristName and lastName. Example database screenshot:

firebase database data

Android Firebase dependency configuration

In this section, we will apply all configurations needed to run Android app.

Android build.gradle (project) file configuration

Under dependencies section of the project build.gradle add google-services:3.1.0.

dependencies {
    ....
    classpath 'com.google.gms:google-services:3.1.0'
}

Add repository in project build.gradle file.

allprojects {
    repositories {
        ....
        maven {
            url "https://maven.google.com"
        }
    }
}

Android build.gradle (module) file configuration

Under module build.gradle firebase-core:11.4.0, firebase-database:11.4.0 and firebase-auth:11.4.0.

dependencies {
    ....
    compile 'com.google.firebase:firebase-core:11.4.0'
    compile 'com.google.firebase:firebase-database:11.4.0'
    compile 'com.google.firebase:firebase-auth:11.4.0'
}

Apply plugin under module build.gradle file. Note: you need to app google-services.json file in your app directory before applying the following plugin.

apply plugin: 'com.google.gms.google-services'

Android Firebase Anonymous Authentication

Now we need to initialize firebase authentication into Android app. The following code will initialize and perform anonymous login:

FirebaseOptions options = new FirebaseOptions.Builder().setApplicationId("firebase-android-integration")
        .setDatabaseUrl("https://firebase-android-integration.firebaseio.com/").build();
FirebaseApp app = FirebaseApp.initializeApp(this, options, "firebase-android-integration");
FirebaseAuth auth = FirebaseAuth.getInstance(app);
Task<AuthResult> task = auth.signInAnonymously();

Add code to Main Activity.

Android Firebase Realtime Database Access

Now we are ready to fetch data from firebase real-time database. Following code would fetch user -> user1 JSON data:

FirebaseDatabase database = FirebaseDatabase.getInstance(listener.getFirebaseAppInstance());
DatabaseReference databaseReference = database.getReference("/user/user1");
databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        DatabaseRecord record = dataSnapshot.getValue(DatabaseRecord.class);
        Log.d(TAG, record.getFirstName()+":"+record.getLastName()+":"+record.getFullName());
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
    }
});

Append above code after successful anonymous login.

Conclusion

In this post, we learned how to configure firebase anonymous authentication and real-time database connectivity to fetch data using Andriod App.

Leave a Reply

Your email address will not be published. Required fields are marked *