TechBuzzPro

Firebase Cloud Messaging Push Notifications in Android

· 5 min read · Phones & Tablets

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To implement push notifications in an Android app with Firebase Cloud Messaging (FCM), you add the Firebase SDK, create a notification channel, obtain a registration token, and send messages from the Firebase console or your server. The device then displays notifications when the app is in the background or handles data messages in code. This guide walks through the standard setup using Android Studio and the official Firebase tools.

What You'll Need

  • Android Studio (current stable version) with an Android project targeting a recent API level.
  • A Google account to access the Firebase console.
  • Your app's package name (applicationId) and, if you plan to use Google Sign-In or other Google services, a SHA-1 fingerprint.
  • Internet access on the test device or emulator (emulators with Google Play services are recommended).

Step-by-Step

  1. Create a Firebase project. Go to the Firebase console, click Add project, give it a name, and follow the prompts. You can disable Google Analytics if you do not need it.
  2. Register your Android app. In the Firebase project, click the Android icon to add an app. Enter your package name exactly as it appears in your app's build.gradle (applicationId). Add a nickname and your debug SHA-1 if prompted. Download the generated google-services.json file.
  3. Add the config file. Place google-services.json in your app module's root directory (usually app/). Do not rename it.
  4. Add the Google services plugin. In your project-level build.gradle (or settings.gradle for newer projects), add the Google services plugin classpath. In the app-level build.gradle, apply the plugin at the top. Sync the project.
  5. Add the Firebase Messaging dependency. In the app-level build.gradle dependencies block, add the Firebase BOM and the messaging library. Sync again. The BOM keeps Firebase library versions aligned.
  6. Create a notification channel. On Android 8.0 (API 26) and higher, notifications must belong to a channel. In your main activity or application class, create a NotificationChannel with an ID, name, and importance level (for example, IMPORTANCE_DEFAULT). Register it with the NotificationManager. Do this once; repeated creation is harmless but unnecessary.
  7. Get the registration token. In a service that extends FirebaseMessagingService, override onNewToken to receive token updates. Also call FirebaseMessaging.getInstance().getToken() after the app starts to retrieve the current token. Send this token to your server so it can target the device. Log it during development.
  8. Handle incoming messages. Override onMessageReceived in the same service. For notification messages (messages with a notification payload), the system displays them automatically when the app is in the background; onMessageReceived is called when the app is in the foreground. For data-only messages, onMessageReceived is always called, and you build and show the notification yourself using NotificationCompat, passing the channel ID.
  9. Declare the service. Add the FirebaseMessagingService subclass to your AndroidManifest.xml with an intent filter for com.google.firebase.MESSAGING_EVENT. This is required for the service to receive messages.
  10. Test from the Firebase console. In the Firebase console, go to Messaging, create a campaign or a test message, and target your app. Send a test notification to the registration token you logged. Check that it appears when the app is backgrounded and that onMessageReceived logs when foregrounded.
  11. Send from your server (optional). Use the Firebase Admin SDK or the FCM HTTP v1 API with your server key or service account credentials to send messages to specific tokens or topics. Include notification and data payloads as needed. Never embed server credentials in the app.

Common Mistakes

  • Placing google-services.json in the project root instead of the app module folder.
  • Forgetting to apply the Google services plugin in the app-level build.gradle.
  • Not creating a notification channel on Android 8.0+, so notifications silently fail to appear.
  • Assuming onMessageReceived is called for notification messages when the app is in the background; the system handles those automatically.
  • Using the legacy server key in client code or exposing it in a repository.
  • Testing on an emulator without Google Play services, which cannot receive FCM messages.
  • Not handling token refresh, so messages stop arriving after the token changes.

Tips

  • Use data-only messages when you need full control over notification display and behavior.
  • Store tokens on your server and remove stale tokens when FCM reports them as unregistered.
  • Set a notification icon that renders correctly in the status bar (usually a white silhouette).
  • Group related notifications and set a consistent channel importance to avoid user annoyance.
  • Test on a physical device as well as an emulator; some OEM battery optimizations affect delivery.
  • Keep the Firebase SDK updated via the BOM to benefit from fixes.

Troubleshooting

If notifications do not arrive, work from simplest to most involved. First, confirm the device has internet and Google Play services. Restart the device and the app. Check that google-services.json is in the right place and the plugin is applied. Verify the notification channel exists and its importance is not set to none. Look at logcat for FirebaseMessagingService errors and token retrieval failures. If you send from the console, ensure the target token matches the current one. If you send from a server, check the response for invalid or unregistered tokens. Reinstalling the app generates a new token, so update your server. Clearing app data also resets the token. If you use topics, allow time for subscription propagation. For emulator issues, use an image with Google Play services. If all else fails, create a minimal test project with the same setup to isolate configuration problems.

Frequently Asked Questions

Do I need a server to use FCM?

No. You can send notifications from the Firebase console for testing and simple campaigns. For automated or user-specific messages, a server using the Admin SDK or HTTP v1 API is the standard approach.

Why does onMessageReceived not fire when the app is in the background?

For notification messages, the system displays them automatically in the background and does not call onMessageReceived. Use data-only messages if you need your code to run in that case.

How do I get the FCM registration token?

Call FirebaseMessaging.getInstance().getToken() and handle the task result. Also override onNewToken in your FirebaseMessagingService to receive updates.

Can I send notifications to all users?

Yes, by subscribing users to a topic and sending to that topic, or by collecting tokens and sending to each. Topics are simpler for broad audiences.

What is a notification channel?

It is a category that defines how notifications appear and sound on Android 8.0 and higher. Apps must create at least one channel before showing notifications.

Is FCM free?

FCM itself is free to use. Costs may apply for other Firebase services you enable, such as Cloud Functions or Analytics.