Push notifications have become a must-have feature in modern Android apps. Whether it’s alerting users about new messages, sending reminders, or sharing special offers — push notifications keep users engaged even when they are not actively using your app.
If you’re an Android developer (beginner or intermediate), learning how to integrate push notifications using Firebase Cloud Messaging (FCM) is essential. In this article, I’ll explain step-by-step how to add push notifications in your Android app, share code examples, and include working backlinks to official documentation for deeper reading.
What Are Push Notifications?
Push notifications are messages sent directly from your server (or Firebase) to your app users’ devices — even when the app isn’t running.
They can be:
- Text alerts (like “You’ve got a new message!”)
- Rich media (images, buttons, links)
- Actionable (users can reply or tap a button)
Push notifications help increase engagement, retention, and reactivation of inactive users.
More details: Google Developers – Android Notifications Overview
Why Use Firebase Cloud Messaging (FCM)?
Firebase Cloud Messaging (FCM) is Google’s official, free service to send push notifications on Android, iOS, and web apps.
Benefits of using FCM:
- It’s completely free.
- Scalable to millions of users.
- Supports both notification & data messages.
- Works with Android Studio directly.
- Easy integration with Firebase console.
Official docs: Firebase Cloud Messaging
Step 1: Create a New Android Studio Project
- Open Android Studio.
- Select New Project → Empty Activity.
- Give your app a name like
PushNotificationDemo. - Choose Language: Java or Kotlin.
- Click Finish.
Wait for Gradle to finish building your project.
Step 2: Create a Firebase Project
Now you need to create a Firebase project to connect with your app.
- Go to Firebase Console.
- Click Add Project → Enter Project Name.
- Click Continue, skip Google Analytics (optional).
- Once the project is created, click Add App → Android.
- Enter your Android package name (found in
AndroidManifest.xml).
After registration, download the google-services.json file and place it inside your app’s /app directory.
Step 3: Add Firebase Dependencies
Open your app-level build.gradle file and add the following lines under dependencies:
implementation 'com.google.firebase:firebase-messaging:24.0.0'
implementation 'com.google.firebase:firebase-analytics:22.0.0'
In the project-level build.gradle, ensure you have:
classpath 'com.google.gms:google-services:4.4.2'
Then, at the bottom of your app-level build.gradle, add:
apply plugin: 'com.google.gms.google-services'
Click Sync Now to download the dependencies.
For latest versions, check the Firebase Android Setup Guide.
Step 4: Set Up Firebase Cloud Messaging
Once your app is connected to Firebase:
- Go to your Firebase project dashboard.
- Navigate to Cloud Messaging → Send your first message.
- Enter a notification title and message.
- Choose your app under “Target” and click Send Message.
If everything is configured properly, your device will receive the message automatically once the app is installed.
Step 5: Handle Notifications in the App
When the app receives a message from FCM, you can handle it in your own class extending FirebaseMessagingService.
Example in Java:
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import android.util.Log;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("FCM", "From: " + remoteMessage.getFrom());
if (remoteMessage.getNotification() != null) {
String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
Log.d("FCM", "Message: " + message);
}
}
@Override
public void onNewToken(String token) {
super.onNewToken(token);
Log.d("FCM", "Refreshed token: " + token);
}
}
This code listens for incoming push notifications and logs the data.
For detailed documentation: Firebase Messaging Service
Step 6: Create a Custom Notification
If you want to show your own notification layout instead of Firebase’s default one, use this code:
private void showNotification(String title, String message) {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = "my_channel_id";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
channelId, "My Channel", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.drawable.ic_notification)
.setAutoCancel(true);
manager.notify(0, builder.build());
}
Now call this method inside onMessageReceived() when you get a notification payload.
Step 7: Get the Device Token
Every device that installs your app gets a unique FCM token — this is how Firebase identifies where to send the message.
To get the token, use:
FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(task -> {
if (!task.isSuccessful()) {
Log.w("FCM", "Fetching FCM registration token failed", task.getException());
return;
}
String token = task.getResult();
Log.d("FCM", "Token: " + token);
});
You can send this token to your backend server if you want to target specific users later.
Step 8: Test Sending Notifications via Firebase Console
Once your app is running, test notifications:
- Open Firebase Console → Cloud Messaging.
- Click Send Message.
- Enter title, message, and optionally an image.
- Click Send Test Message and enter your device token.
If everything works, you’ll see a push notification appear on your Android device.
Step 9: Advanced — Sending Notifications from Server
Instead of using Firebase console manually, you can send push notifications programmatically from your backend using the Firebase Admin SDK or REST API.
Example with cURL:
curl -X POST -H "Authorization: key=YOUR_SERVER_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": "DEVICE_TOKEN",
"notification": {
"title": "Server Test",
"body": "This message came from your backend!"
}
}' \
https://fcm.googleapis.com/fcm/send
Full reference: Firebase Cloud Messaging HTTP API
Step 10: Troubleshooting Common Errors
| Issue | Possible Cause | Fix |
|---|---|---|
| No notifications received | Device not registered properly | Check google-services.json and re-sync Gradle |
| “Failed to fetch token” error | Network or Firebase project issue | Make sure internet permission is added |
| Notification works in background only | Payload issue | Use both “notification” and “data” fields in message |
| Duplicates | App is showing system + custom notifications | Disable auto-notification display in manifest |
For full list: Firebase Troubleshooting Guide
Step 11: Make Notifications Clickable
You can make notifications interactive by opening a specific Activity when clicked.
Add PendingIntent like this:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "my_channel_id")
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
Now when users tap on the notification, your app will open to the desired screen.
Step 12: Enable Notifications Permission (Android 13+)
If you target Android 13 (API 33) or higher, you must request runtime permission for notifications:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
Then, in your activity:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.POST_NOTIFICATIONS}, 101);
}
See: Android Notification Permission Guide
Best Practices
- Don’t spam — too many notifications will make users uninstall your app.
- Segment users — send relevant messages to each user group.
- Test both foreground and background delivery.
- Use rich notifications (images, actions) for higher engagement.
- Always include a “turn off notifications” option inside app settings.
Final Thoughts
Integrating push notifications into your Android app may seem complex, but Firebase makes it surprisingly easy and free. Once you understand the flow — from Firebase setup to device token management — you can use notifications to greatly increase user engagement.
Remember: Push notifications are powerful when used smartly. Keep them meaningful, timely, and respectful of users’ attention.
If you follow the steps above, your app will be ready to send and receive notifications in less than an hour.