Use FCM and FIAM to send messages to users

1. Before you begin

In this codelab, you learn how to add the ability to engage with users whether they are using your app in the foreground or your app is in the background.

206c7ecece550bde.png

Prerequisites

None

What you'll learn

  • Add Firebase to your Android App
  • Add FCM and FIAM dependencies
  • Send test FCM message to your app
  • Send test FIAM message to your app

What you'll need

  • Android Studio 4.1
  • Android device or emulator

2. Get started

Get the sample code

Clone the GitHub repository from the command line:

Import the starter app

From Android Studio, select the codelab-fcm-and-fiam directory ( android_studio_folder.png). It would be the one cloned in the previous step (File > Open > .../codelab-fcm-and-fiam).

You should now have the FcmAndFiam project open in Android Studio. If you see a warning about a google-services.json file missing, don't worry. It will be added in the next step.

3. Create Firebase console project

Add Firebase to the project

  1. Go to the Firebase console.
  2. Select Add project.
  3. Select or enter a Project name.
  4. Follow the remaining setup steps in the Firebase console, then click Create project (or Add Firebase, if you're using an existing Google project).
  5. From the overview screen of your new project, click the Android icon to launch the setup workflow.
  6. Enter the codelab's package name: com.google.firebase.codelab.fcmandfiam

Add google-services.json file to your app

After adding the package name click Register App then Download google-services.json to obtain your Firebase Android config file then copy the google-services.json file into the app directory in your project. After the file is downloaded you can click Skip for the next steps shown in the console (they've already been done for you in the build-android-start project).

Add google-services plugin to your app

The google-services plugin uses the google-services.json file to configure your application to use Firebase. The google-services plugin dependency and the plugin itself should already be added to the project and app level build files respectively. Confirm the following entries:

build.gradle

buildscript {
  ...
  dependencies {
    ...
    classpath 'com.google.gms:google-services:4.3.8'
  }
}

app/build.gradle

plugins {
  ...
  id: 'com.google.gms.google-services'
}

Add dependencies

FCM and FIAM require the following dependencies. These dependencies should already be added to the app level build file. Confirm the following dependencies are added:

app/build.gradle

dependencies {
  ...
  implementation platform('com.google.firebase:firebase-bom:28.4.2')
  implementation 'com.google.firebase:firebase-analytics'
  implementation 'com.google.firebase:firebase-messaging'
  implementation 'com.google.firebase:firebase-inappmessaging-display'
}

Sync your project with gradle files

To be sure that all dependencies are available to your app, you should sync your project with gradle files at this point. Select File > Sync Project with Gradle Files from the Android Studio toolbar.

4. Log Identifiers

Firebase Cloud Messaging and Firebase In App Messaging both use identifiers to send messages to apps. FCM uses a registration token and FIAM uses an installation ID.

  1. In MainActivity.kt replace the TODO with the code below to log the identifiers needed to send messages:
FirebaseMessaging.getInstance().token.addOnCompleteListener { regTokenTask ->
   if (regTokenTask.isSuccessful) {
       Log.d(TAG, "FCM registration token: ${regTokenTask.result}")
   } else {
       Log.e(TAG, "Unable to retrieve registration token",
           regTokenTask.exception)
   }
}
FirebaseInstallations.getInstance().id.addOnCompleteListener { installationIdTask ->
   if (installationIdTask.isSuccessful) {
       Log.d(TAG, "Firebase Installations ID: ${installationIdTask.result}")
   } else {
       Log.e(TAG, "Unable to retrieve installations ID",
           installationIdTask.exception)
   }
}
  1. Connect your Android device, and click Run ( execute.png)in the Android Studio toolbar. Tapping on the Log Identifiers button will log the FCM registration token and the Firebase Installations ID to logcat. The output should look like this:
D/FcmAndFiam: Firebase Installations ID: emMc5...AsJfb
  FCM registration token: emMc5LART6GV7...r0Q

5. Send test FCM message

FCM messages can be sent both from the Firebase console and from the FCM REST API. In this codelab we will compose an FCM notification message and send it to our device. Notification messages are those that are displayed in the system's notification area. Send a test FCM message by:

  1. Ensure app is in the background (go to home screen)
  2. Go to the Firebase console and select Cloud Messaging in the left navigation
  3. Select Send your first message
  4. Add a title and body for the message, then click Send test message

207ced65f5245ada.png

  1. Add the FCM registration token, then click test

f3556c6a8a6453fe.png

  1. Observe display notification on device
  2. Tapping on the notification opens the app

6. Send test FIAM message

Firebase In App Messaging can be triggered as a result of a variety of user events. The test message uses the app open event to trigger the In App Message. Send a test FIAM message by:

  1. Ensure app is in the background (go to home screen)
  2. Go to the Firebase console and select In-App Messaging in the left navigation
  3. Select Create your first campaign
  4. Select top banner campaign and enter title and body of the message
  5. Click Test on device

69f6ea5efff240d1.png

  1. Add Installations ID and click Test

24b101edfab47144.png

  1. Reopen app
  2. Observe in app message

7. Congratulations

Congratulations, you've successfully sent both FCM and FIAM messages. Now you know how to engage with your users whether they are using your app in the foreground or your app is in the background.

Further reading

What's Next

  • Send messages to users in your own Android app.

Reference docs