How to Handle Notification When App in Background in Firebase Android?

Android, Android Apps

In this tutorial, we will learn how to handle notifications when an Android app is in the background using Firebase. Firebase Cloud Messaging (FCM) allows you to send push notifications to your app users, even when the app is not actively running in the foreground.

Prerequisites

Before we begin, make sure you have the following:

  • Android Studio installed on your machine
  • A Firebase project set up
  • The latest version of the Firebase Cloud Messaging dependency added to your app’s build.gradle file

Step 1: Set up Firebase Cloud Messaging

The first step is to set up Firebase Cloud Messaging in your Android app. Follow these steps:

  1. Create a new project in the Firebase console and add your Android app to it.
  2. Download the google-services.json file and place it inside your Android project’s app folder.
  3. Add the necessary dependencies to your app’s build.gradle file.
  4. In your app’s manifest file, add the required permissions for FCM.

Step 2: Handle Notifications in Background

To handle notifications when your app is in the background, you need to create a service that extends FirebaseMessagingService. This service will be responsible for receiving and handling incoming messages.

Create a new Java class called MyFirebaseMessagingService.java and add the following code:


import com.google.firebase.messaging.FirebaseMessagingService;
import com.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        // Handle the incoming notification here
        // You can access the notification data using remoteMessage.getData()
    }
}

Make sure to register this service in your app’s manifest file:


<service
    android:name=".MyFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.MESSAGING_EVENT" />
    </intent-filter>
</service>

Step 3: Customize Notification Handling

By default, Firebase will handle the display of notifications when your app is in the background. However, you can customize this behavior by creating a class that extends FirebaseMessagingService and overriding the onMessageReceived() method.

In your MyFirebaseMessagingService class, add the following code to customize the notification handling:


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    // Check if the message contains a notification payload
    if (remoteMessage.getNotification() != null) {
        // Get the title and body of the notification
        String title = remoteMessage.getNotification().getTitle();
        String body = remoteMessage.getBody();

        // Customize how you want to handle the notification here
        // You can show a custom dialog, play a sound, etc.
        
        // Example: Show a toast message with the title and body of the notification
        Toast.makeText(getApplicationContext(), "New Notification: " + title + "\n" + body, Toast.LENGTH_LONG).show();
    }
}

Step 4: Test the Notification

Now that you have implemented the necessary code to handle notifications when your app is in the background, it’s time to test it.

  1. Open your Firebase console and go to the “Cloud Messaging” tab.
  2. Click on “New Message” and enter the notification details.
  3. Select your app’s package name from the Target dropdown.
  4. Click on “Send” to send the notification.

You should now see the customized notification handling in action when your app is in the background!

Summary

In this tutorial, we learned how to handle notifications when an Android app is in the background using Firebase Cloud Messaging. We set up Firebase Cloud Messaging, created a service to handle incoming messages, customized the notification handling, and tested our implementation. Now you can deliver important updates and information to your users even when they are not actively using your app!