Using Firebase Cloud Messaging with Android O

Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably deliver messages to your apps and sites. It provides two types of messages:

  1. Notification Messages display a simple notification popup, with optional data payload.
  2. Data Messages deliver a JSON payload to your application and let your code handle it.

Data Messages are a great way to build custom notifications, when the layout provided by notification messages is not enough, or to trigger background operations like a database sync or the download of additional content (image attachments, emails, etc.)

How should Data Messages trigger background operations?

The best way to trigger a background operation from a data message is by using the Work Manager to schedule your operation at a point when it’s best for the user (like avoiding extra work when the battery is very low, or when the CPU is already heavily used by other foreground applications).

Background Process Optimizations in Android O

To get started, check out the Android O Developer Preview site where you will find instructions on downloading and installing the required SDKs. For Firebase Development, you’ll also need to install the Firebase SDKs for Android. Be sure to use version 10.2.1 or later for Android O development.

Android O introduces new background processes optimizations, which make the use of JobScheduler (or wrapper libraries like the Work Manager) a requirement for long-running background operations. Due to these optimizations, the FCM (hence GCM as well) callbacksonMessageReceived()and onTokenRefresh() have a guaranteed life cycle limited to 10 seconds (same as a Broadcast Receiver). After the guaranteed period of 10 seconds, Android considers your process eligible for termination, even if your code is still executing inside the callback. To avoid your process being terminated before your callback is completed, be sure to perform only quick operations (like updating a local database, or displaying a custom notification) inside the callback, and use JobScheduler to schedule longer background processes (like downloading additional images or syncing the database with a remote source).

public void onMessageReceived(RemoteMessage remoteMessage) {if (/* Check if data needs to be processed by long running job */ true) {// For long-running tasks (10 seconds or more) use the Work Manager
 } else {// Handle message within 10 seconds
 * Schedule a job using the Work Manager.
private void scheduleJob() {WorkManager.getInstance().enqueue(
      new OneTimeWorkRequest.Builder(MyWorker.class).build());
 * Perform and immediate, but quick, processing of the message.
private void handleNow() {Log.d(TAG, "Short lived task is done.");

We hope this helps you understand how to use FCM to schedule long-running background operations. This solution greatly helps Android to preserve battery life and ensures that your application works fine on Android O. In case you have any questions don’t hesitate to ask us on our support channels.