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.
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.
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.
| 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 |
dependencies { implementation 'com.google.firebase:firebase-messaging:23.4.0' }
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); } }
Each device receives a unique token used to send targeted notifications.
@Override public void onNewToken(String token) { Log.d("FCM_TOKEN", token); }
| 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.
Yes, Firebase Cloud Messaging is completely free and supports unlimited notifications.
Yes, FCM works even when the app is closed or running in the background.
Notification messages are handled automatically by Android, while data messages are processed by app logic.
Yes, FCM uses secure device tokens and encrypted communication.
Yes, Firebase Console allows you to send test notifications without a backend.
Copyrights © 2024 letsupdateskills All rights reserved