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 samples 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.
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.
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).
(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.
Click Register app.
Add a Firebase configuration file
Add the Firebase Android configuration file to your app:
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.
- Make sure the config file is not appended with additional
characters, like
(2).
Move your config file into the module (app-level) directory of your app.
To enable Firebase products in your app, add the google-services plugin to your Gradle files.
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 { repositories { // Check that you have the following line (if not, add it): google() // Google's Maven repository } dependencies { // ... // Add the following line: classpath 'com.google.gms:google-services:4.3.2' // Google Services plugin } } allprojects { // ... repositories { // Check that you have the following line (if not, add it): google() // Google's Maven repository // ... } }In your module (app-level) Gradle file (usually
app/build.gradle), add a line to the bottom of the file.apply plugin: 'com.android.application' android { // ... } // Add the following line to the bottom of the file: apply plugin: 'com.google.gms.google-services' // Google Play services Gradle plugin
Add Firebase SDKs to your app
You can add any of the supported Firebase products to your Android 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.
Analytics enabled
dependencies { // ...
// Add the Firebase SDK for Google Analytics implementation 'com.google.firebase:firebase-analytics:17.2.0'
// Add the SDK for Firebase Cloud Messaging implementation 'com.google.firebase:firebase-messaging:20.0.0'
// Getting a "Could not find" error? Make sure that you've added // Google's Maven repository to your root-level build.gradle file }Analytics not enabled
dependencies { // ...
// Add the SDK for Firebase Cloud Messaging implementation 'com.google.firebase:firebase-messaging:20.0.0'
// Getting a "Could not find" error? Make sure that you've added // Google's Maven repository to your root-level build.gradle file }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.
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():
Java
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "getInstanceId failed", task.getException());
return;
}
// 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();
}
});
Kotlin
FirebaseInstanceId.getInstance().instanceId
.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
Log.w(TAG, "getInstanceId failed", task.exception)
return@OnCompleteListener
}
// 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.
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)
}
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
Install and run the app on the target device.
Make sure the app is in the background on the device.
Open the Notifications composer and select New Message.
Enter the message text.
Select Send test message.
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.
Go beyond notification messages
To go beyond notification messages and add other, more advanced behavior to your app, see:

