Push Notification in Android using Firebase Cloud Messaging (FCM)

Push notifications are a powerful communication tool that allows Android applications to deliver messages directly to users, even when the app is not actively running. In this detailed guide, you will learn how to implement Push Notification in Android using Firebase Cloud Messaging (FCM) with real-world examples, clear explanations, and practical code samples.

FCM works by creating a reliable connection between your server and devices, allowing you to send notifications with custom content, data, or both, handling display in the background and letting foreground apps manage behavior, ensuring reliable, cross-platform communication for re-engagement.

This article is suitable for beginners and intermediate Android developers who want to understand the complete workflow of Firebase Cloud Messaging.

What is a Push Notification in Android?

A push notification is a short message sent from a server to an Android device. These notifications appear on the notification tray, lock screen, or as banners, helping apps engage users in real time.

Key Features of Push Notifications

  • Delivered instantly
  • Works even when the app is closed
  • Triggered by server-side events
  • Improves user engagement and retention

Common Real-World Examples

  • Chat message alerts
  • Order and delivery updates
  • Banking and payment notifications
  • Promotional offers and reminders

What is Firebase Cloud Messaging (FCM)?

Firebase Cloud Messaging (FCM) is a cross-platform messaging service provided by Google. It enables developers to send push notifications and data messages to Android apps efficiently and securely.

Why Choose Firebase Cloud Messaging?

  • Free and reliable service
  • Easy integration with Android Studio
  • Supports notification and data payloads
  • Automatic device token management
  • High scalability and performance

How Firebase Cloud Messaging Works

FCM Workflow

  1. The Android app registers with Firebase
  2. Firebase generates a unique device token
  3. The token is stored on the backend server
  4. The server sends a message request to FCM
  5. FCM delivers the notification to the device

Types of FCM Messages

Message Type Description Use Case
Notification Message Handled automatically by Android Simple alerts
Data Message Handled by app code Custom logic
Combined Message Includes notification and data Advanced scenarios

Real-World Use Cases of Android Push Notifications

  • E-commerce: Order status, discounts, cart reminders
  • Social Media: Likes, comments, friend requests
  • Finance: Transaction alerts and payment reminders
  • Education: Exam schedules and assignment alerts
  • Healthcare: Appointment reminders and reports

Prerequisites for Implementing Firebase Cloud Messaging

Required Tools

  • Android Studio
  • Firebase account
  • Android device or emulator

Basic Knowledge Needed

  • Android app development fundamentals
  • Activities and Services
  • Basic Java or Kotlin

Step-by-Step Implementation of Push Notification in Android

Step 1: Create a Firebase Project

  • Open Firebase Console
  • Create a new project
  • Add your Android app package name
  • Download the google-services.json file

Step 2: Add Firebase Dependencies

dependencies { implementation 'com.google.firebase:firebase-messaging:23.4.0' }

Step 3: Enable Firebase Cloud Messaging

  • Go to Firebase Console
  • Open Cloud Messaging
  • Enable the service

Creating Firebase Messaging Service

To receive messages, create a service that extends FirebaseMessagingService.

public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) { if (remoteMessage.getNotification() != null) { String title = remoteMessage.getNotification().getTitle(); String body = remoteMessage.getNotification().getBody(); showNotification(title, body); } } private void showNotification(String title, String message) { NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification.Builder(this) .setContentTitle(title) .setContentText(message) .setSmallIcon(R.drawable.ic_notification) .build(); manager.notify(1, notification); } }

Explanation

  • onMessageReceived triggers when a message arrives
  • RemoteMessage holds notification data
  • NotificationManager displays the alert

Handling Firebase Device Token

Each device receives a unique token used to send targeted notifications.

@Override public void onNewToken(String token) { Log.d("FCM_TOKEN", token); }

Token Best Practices

  • Send token to backend server
  • Update token when refreshed
  • Store securely

Sending Push Notifications from Firebase Console

  • Open Firebase Console
  • Navigate to Cloud Messaging
  • Create a new notification
  • Enter title and message
  • Send to test device

Common Issues and Solutions

Issue Solution
Notification not received Check device token and internet
App crash on notification Validate payload format
No notification display Create notification channels


Implementing Push Notification in Android using Firebase Cloud Messaging helps create engaging, real-time experiences for users. Firebase makes the process simple, scalable, and reliable. By following the steps, examples, and best practices outlined in this guide, you can confidently integrate push notifications into your Android applications.

Frequently Asked Questions (FAQs)

1. Is Firebase Cloud Messaging free to use?

Yes, Firebase Cloud Messaging is completely free and supports unlimited notifications.

2. Can FCM deliver notifications when the app is closed?

Yes, FCM works even when the app is closed or running in the background.

3. What is the difference between notification and data messages?

Notification messages are handled automatically by Android, while data messages are processed by app logic.

4. Is Firebase Cloud Messaging secure?

Yes, FCM uses secure device tokens and encrypted communication.

5. Can I send notifications without a backend server?

Yes, Firebase Console allows you to send test notifications without a backend.

line

Copyrights © 2024 letsupdateskills All rights reserved