Firebase With Java
Firebase With Java
Guide
Introduction
Firebase is a Backend-as-a-Service (BaaS) platform developed by Google that provides
various tools and services to help you build high-quality applications. This guide will cover
the essential steps to integrate Firebase with Java, specifically focusing on Firestore,
Authentication, and Cloud Functions.
Prerequisites
Before you begin, ensure you have:
A Firebase account.
Java Development Kit (JDK) installed on your machine.
An Integrated Development Environment (IDE) like IntelliJ IDEA or Eclipse.
1. Setting Up Firebase
1.1 Create a Firebase Project
If you’re building a web app, click on the web icon (</>) to add your application.
For Java-based backend services, you may not need a specific app registration, but you will
need to obtain the google-services.json file for Android apps or use Firebase Admin
SDK for server-side applications.
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>9.0.0</version> <!-- Check for the latest version -->
</dependency>
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import java.io.FileInputStream;
import java.io.IOException;
FirebaseApp.initializeApp(options);
System.out.println("Firebase initialized successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
4. Using Firebase Services
4.1 Firestore
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-firestore</artifactId>
<version>3.12.0</version> <!-- Check for the latest version -->
</dependency>
import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.FirestoreOptions;
import com.google.api.core.ApiFuture;
import com.google.cloud.firestore.DocumentReference;
import com.google.cloud.firestore.WriteResult;
import com.google.cloud.firestore.QueryDocumentSnapshot;
import com.google.cloud.firestore.QuerySnapshot;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
// Retrieve documents
ApiFuture<QuerySnapshot> query = db.collection("users").get();
List<QueryDocumentSnapshot> documents = query.get().getDocuments();
for (QueryDocumentSnapshot document : documents) {
System.out.println("Document data: " + document.getData());
}
}
}
4.2 Authentication
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-auth</artifactId>
<version>9.0.0</version> <!-- Check for the latest version -->
</dependency>
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.UserRecord;
import com.google.firebase.auth.UserRecord.CreateRequest;
UserRecord userRecord =
FirebaseAuth.getInstance().createUser(request);
System.out.println("Successfully created new user: " +
userRecord.getUid());
} catch (Exception e) {
e.printStackTrace();
}
}
}
5. Cloud Functions
To use Cloud Functions, set up Firebase CLI and write your functions in JavaScript.
However, you can trigger Cloud Functions from your Java app using HTTP requests.
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
Conclusion
This document has provided a comprehensive overview of integrating Firebase with Java,
covering initialization, Firestore usage, authentication, and invoking Cloud Functions. As you
develop your application, refer to the Firebase Documentation for further information and best
practices.