Using Firebase with Jetpack Compose Authentication, Realtime DB, and Push Notifications
✨ Introduction
In 2025, building modern Android apps is faster and more scalable than ever — especially when you combine Jetpack Compose with Firebase. Whether you’re developing a chat app, a real-time dashboard, or a social platform, Firebase provides essential backend services without the hassle of managing your own infrastructure.
This tutorial walks you through:
- Setting up Firebase Authentication (Email & Password)
- Reading and writing with Firebase Realtime Database
- Sending and receiving Push Notifications using Firebase Cloud Messaging (FCM)
- Seamless integration with Jetpack Compose UI
By the end, you’ll have a ready-to-expand app powered by Compose and Firebase — production-friendly and scalable.
🛠️ Part 1: Setting Up Firebase in Your Android Project
Step 1: Create a Firebase Project
- Go to Firebase Console
- Click “Add project”
- Follow the prompts and skip Google Analytics (optional)
Step 2: Connect Your Android App
- Add your app’s package name
- Download google-services.json and place it in app/ directory
Step 3: Add Firebase SDKs in
Define Firebase BOM and SDKs in libs.versions.toml
[versions]
firebase-bom = "32.7.0"
firebase-auth = "22.3.0"
firebase-db = "20.3.0"
firebase-messaging = "23.3.1"
[libraries]
firebase-bom = { module = "com.google.firebase:firebase-bom", version.ref = "firebase-bom" }
firebase-auth = { module = "com.google.firebase:firebase-auth-ktx", version.ref = "firebase-auth" }
firebase-db = { module = "com.google.firebase:firebase-database-ktx", version.ref = "firebase-db" }
firebase-messaging = { module = "com.google.firebase:firebase-messaging-ktx", version.ref = "firebase-messaging" }
build.gradle (module-level)
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'com.google.gms.google-services'
}
dependencies {
implementation platform(libs.firebase.bom)
implementation libs.firebase.auth
implementation libs.firebase.db
implementation libs.firebase.messaging
}
Add this line at the bottom of your build.gradle (app-level):
apply plugin: 'com.google.gms.google-services'
Sync the project — you’re ready to build.
🔐 Part 2: Firebase Authentication with Jetpack Compose
We’ll build a simple login screen and authenticate with email/password.
UI: Login Screen in Compose
@Composable
fun LoginScreen(viewModel: AuthViewModel) {
val email = remember { mutableStateOf("") }
val password = remember { mutableStateOf("") }
Column(modifier = Modifier.padding(16.dp)) {
TextField(value = email.value, onValueChange = { email.value = it }, label = { Text("Email") })
TextField(value = password.value, onValueChange = { password.value = it }, label = { Text("Password") }, visualTransformation = PasswordVisualTransformation())
Button(onClick = {
viewModel.signIn(email.value, password.value)
}) {
Text("Login")
}
}
}
ViewModel: Auth Logic
class AuthViewModel : ViewModel() {
private val auth = FirebaseAuth.getInstance()
fun signIn(email: String, password: String) {
auth.signInWithEmailAndPassword(email, password)
.addOnSuccessListener {
Log.d("Auth", "Login successful")
}
.addOnFailureListener {
Log.e("Auth", "Login failed: ${it.message}")
}
}
}
You can add registration and logout functions similarly.
🔄 Part 3: Reading & Writing with Firebase Realtime Database
Data Model
data class UserMessage(val id: String = "", val text: String = "", val timestamp: Long = 0)
Writing Data
fun sendMessage(message: UserMessage) {
val ref = FirebaseDatabase.getInstance().getReference("messages")
ref.child(message.id).setValue(message)
}
Reading Data in Real-Time
fun observeMessages(onUpdate: (List<UserMessage>) -> Unit) {
val ref = FirebaseDatabase.getInstance().getReference("messages")
ref.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
val messages = snapshot.children.mapNotNull { it.getValue(UserMessage::class.java) }
onUpdate(messages)
}
override fun onCancelled(error: DatabaseError) {
Log.e("DB", "Database error: ${error.message}")
}
})
}
Display in Compose
@Composable
fun MessageList(messages: List<UserMessage>) {
LazyColumn {
items(messages) { message ->
Text("${message.text} - ${Date(message.timestamp)}")
}
}
}
📲 Part 4: Push Notifications with Firebase Cloud Messaging (FCM)
Step 1: Enable FCM in Firebase Console
- Go to Firebase Console → Cloud Messaging
- Follow setup instructions
Step 2: Add Service
class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
// Handle notification
Log.d("FCM", "Message: ${remoteMessage.notification?.body}")
}
}
Register this in AndroidManifest.xml:
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Step 3: Get Device Token
FirebaseMessaging.getInstance().token
.addOnSuccessListener { token ->
Log.d("FCM", "Token: $token")
}
Use this token on your server to send push notifications.
🧪 Bonus: Sending Push Notifications from Firebase Console
- Go to Firebase Console → Cloud Messaging
- Click “Send your first message”
- Fill in the title/body and select your app
- Use the token or send to all users
Great for testing your FCM setup before writing server logic.
🚀 Final Thoughts
By combining Firebase with Jetpack Compose, you’re building a modern, reactive, and scalable Android app — without worrying about backend infrastructure.
This combination is perfect for:
- Real-time apps (chat, dashboards, collaboration)
- Authentication-heavy apps
- Projects that need fast prototyping or global scale
Firebase takes care of the backend, while Jetpack Compose empowers you to deliver beautiful UI with less code.