Send a test message to a backgrounded app

To get started with FCM, build out the simplest use case: sending a test notification message from the Notifications composer to a development device when the app is in the background on the device. This page lists all the steps to achieve this, from setup to verification — it may cover steps you already completed if you have set up an Android client app for FCM.

Set up the SDK

This section covers tasks you may have completed if you have already enabled other Firebase features for your app.

Before you begin

  • Install or update Android Studio to its latest version.

  • Ensure that your Android app:

    • Targets API level 16 (Jelly Bean) or later
    • Uses Gradle 4.1 or later
  • Set up a device or emulator for running your app.

    • Emulators must use an emulator image with Google Play.
  • Sign into Firebase using your Google account.

If you don't already have an Android app project, you can download one of our quickstart examples if you just want to try out a Firebase product.

Create a Firebase project

Before you can add Firebase to your Android app, you need to create a Firebase project to connect to your Android app. Visit Understand Firebase Projects to learn more about Firebase projects.

Register your app with Firebase

After you have a Firebase project, you can add your Android app to it.

Visit Understand Firebase Projects to learn more about best practices and considerations for adding apps to a Firebase project, including how to handle multiple build variants.

  1. In the center of the Firebase console's project overview page, click the Android icon to launch the setup workflow.

    If you've already added an app to your Firebase project, click Add app to display the platform options.

  2. Enter your app's application ID in the Android package name field.

    • An application ID is sometimes referred to as a package name.

    • Find this application ID in your module (app-level) Gradle file, usually app/build.gradle (example ID: com.yourcompany.yourproject).

  3. (Optional) Enter other app information as prompted by the setup workflow.

    The nickname is an internal, convenience identifier and is only visible to you in the Firebase console.

  4. Click Register app.

Add a Firebase configuration file

  1. Add the Firebase Android configuration file to your app:

    1. Click Download google-services.json to obtain your Firebase Android config file (google-services.json).

      You can download your Firebase Android config file again at any time.

    2. Move your config file into the module (app-level) directory of your app.

  2. To enable Firebase products in your app, add the google-services plugin to your Gradle files.

    1. In your root-level (project-level) Gradle file (build.gradle), add rules to include the Google Services plugin. Check that you have Google's Maven repository, as well.

      buildscript {
        // ...
        dependencies {
          // ...
          // Add the following line:
          classpath ''  // Google Services plugin
      allprojects {
        // ...
        repositories {
          // Check that you have the following line (if not, add it):
          google()  // Google's Maven repository
          // ...
    2. In your module (app-level) Gradle file (usually app/build.gradle), add a line to the bottom of the file.

      apply plugin: ''
      android {
        // ...
      // Add the following line to the bottom of the file:
      apply plugin: ''  // Google Play services Gradle plugin

Add Firebase SDKs to your app

You can add any of the supported Firebase products to your Android app. We recommend starting with the core Firebase SDK (, which provides Google Analytics for Firebase functionality.

  1. In your module (app-level) Gradle file (usually app/build.gradle), add the dependency for the core Firebase SDK:

    dependencies {
     // ...
     implementation ''
     // Getting a "Could not find" error? Make sure that you've added
     // Google's Maven repository to your root-level build.gradle file
  2. (Optional) Add the dependencies for the other Firebase libraries that you want to use.

    Some Firebase SDKs for Android offer an alternative Kotlin extensions library.

  3. Sync your app to ensure that all dependencies have the necessary versions.

  4. Run your app to send verification to Firebase that you've successfully integrated Firebase.

    Your device logs will display the Firebase verification that initialization is complete. If you ran your app on an emulator that has network access, the Firebase console notifies you that your app connection is complete.

Access the registration token

To send a message to a specific device, you need to know that device's registration token. Because you'll need to enter the token in a field in the Notifications console to complete this tutorial, make sure to copy the token or securely store it after you retrieve it.

On initial startup of your app, the FCM SDK generates a registration token for the client app instance. If you want to target single devices or create device groups, you'll need to access this token by extending FirebaseMessagingService and overriding onNewToken.

This section describes how to retrieve the token and how to monitor changes to the token. Because the token could be rotated after initial startup, you are strongly recommended to retrieve the latest updated registration token.

The registration token may change when:

  • The app deletes Instance ID
  • The app is restored on a new device
  • The user uninstalls/reinstall the app
  • The user clears app data.

Retrieve the current registration token

When you need to retrieve the current token, call FirebaseInstanceId.getInstance().getInstanceId():


        .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
            public void onComplete(@NonNull Task<InstanceIdResult> task) {
                if (!task.isSuccessful()) {
                    Log.w(TAG, "getInstanceId failed", task.getException());

                // Get new Instance ID token
                String token = task.getResult().getToken();

                // Log and toast
                String msg = getString(R.string.msg_token_fmt, token);
                Log.d(TAG, msg);
                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();


        .addOnCompleteListener(OnCompleteListener { task ->
            if (!task.isSuccessful) {
                Log.w(TAG, "getInstanceId failed", task.exception)

            // Get new Instance ID token
            val token = task.result?.token

            // Log and toast
            val msg = getString(R.string.msg_token_fmt, token)
            Log.d(TAG, msg)
            Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()

Monitor token generation

The onNewToken callback fires whenever a new token is generated.


 * Called if InstanceID token is updated. This may occur if the security of
 * the previous token had been compromised. Note that this is called when the InstanceID token
 * is initially generated so this is where you would retrieve the token.
public void onNewToken(String token) {
    Log.d(TAG, "Refreshed token: " + token);

    // If you want to send messages to this application instance or
    // manage this apps subscriptions on the server side, send the
    // Instance ID token to your app server.


 * Called if InstanceID token is updated. This may occur if the security of
 * the previous token had been compromised. Note that this is called when the InstanceID token
 * is initially generated so this is where you would retrieve the token.
override fun onNewToken(token: String?) {
    Log.d(TAG, "Refreshed token: $token")

    // If you want to send messages to this application instance or
    // manage this apps subscriptions on the server side, send the
    // Instance ID token to your app server.

After you've obtained the token, you can send it to your app server and store it using your preferred method. See the Instance ID API reference for full detail on the API.

Send a test notification message

  1. Install and run the app on the target device.

  2. Make sure the app is in the background on the device.

  3. Open the Notifications composer and select New Message.

  4. Enter the message text.

  5. Select Test on Device.

  6. In the field labeled Add an FCM registration token, enter the registration token you obtained in a previous section of this guide.

  7. Click Test

After you click Test, the targeted client device (with the app in the background) should receive the notification in the system notifications tray .

For insight into message delivery to your app, see the FCM reporting dashboard, which records the number of messages sent and opened on iOS and Android devices, along with data for "impressions" (notifications seen by users) for Android apps.

Next steps

Send messages to foregrounded apps

Once you have successfully sent notification messages while your app is in the background, see Receive Messages in an Android App to get started sending to foregrounded apps.

Go beyond notification messages

To go beyond notification messages and add other, more advanced behavior to your app, see: