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.

Important: This guide focuses on the background case. If you want to receive
messages when your app is in the foreground as well, see also
Receive Messages in an Android App.

Set up the SDK

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

Add Firebase SDKs to your app

To your module (app-level) Gradle file (usually app/build.gradle), add the
dependencies for the Firebase products that you want to use in your app.

For an optimal experience with Firebase Cloud Messaging, we recommend
enabling Google Analytics
in your project. As part of setting up Google Analytics, you need to
add the Firebase SDK for Google Analytics to your app.

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

If you added Analytics, run your app to send verification to Firebase
that you've successfully integrated Firebase. Otherwise, you can skip the
verification step.

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.

Monitor token generation

The onNewToken callback fires whenever a new token is generated.

Java

/**
* 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
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.
sendRegistrationToServer(token);
}

Kotlin

/**
* 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.
sendRegistrationToServer(token)
}

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

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.