Implementing Push Notifications in Android Apps
Q: How do you implement push notifications in an Android app? Can you show an example using Firebase Cloud Messaging?
- Android
- Mid level question
Explore all the latest Android interview questions and answers
ExploreMost Recent & up-to date
100% Actual interview focused
Create Android interview for FREE!
Push notifications are a powerful way to keep users engaged
with your Android app by sending them timely updates and messages. Firebase
Cloud Messaging (FCM) is a popular service for implementing push notifications
in Android apps, and it offers a simple and easy-to-use API.
Here's an example of how to implement push notifications in
an Android app using Firebase Cloud Messaging:
Step 1: Set up Firebase Cloud Messaging in your app
- Create
a Firebase project and add your Android app to it
- Follow
the instructions to add the Firebase SDK to your app
Step 2: Set up the FCM client in your app
- Add
the FCM client library to your app's build.gradle file
- Create
a new class that extends FirebaseMessagingService, and override the
onMessageReceived() method to handle incoming messages
- In the
onMessageReceived() method, you can access the notification data and
create a notification to display to the user
Here's an example of how the onMessageReceived() method might look:
public class MyFirebaseMessagingService extends FirebaseMessagingService { public void onMessageReceived(RemoteMessage remoteMessage) { // Check if the message contains a notification payload. if (remoteMessage.getNotification() != null) { // Create a notification from the message data. NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setContentTitle(remoteMessage.getNotification().getTitle()) .setContentText(remoteMessage.getNotification().getBody()) .setSmallIcon(R.drawable.ic_notification); // Show the notification. NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(0, notificationBuilder.build()); } } }
Step 3: Send push notifications from the Firebase console or
using the FCM API
- You
can use the Firebase console to send notifications to specific devices or
user segments
- You
can also use the FCM API to send notifications programmatically from your
server
That's it! With these steps, you can now send push
notifications to your Android app using Firebase Cloud Messaging.


