By Shubham Aggarwal | 2/18/2017 | General |Beginners

Android - Guide to Firebase JobDispatcher

Android - Guide to Firebase JobDispatcher

This tutorial will guide you in how to use the Firebase Job Dispatcher API which is compatible with the JobScheduler API for scheduling background jobs in your Android app. Let’s explore this exciting API in detail.

Firebase Job dispatcher

The Firebase JobDispatcher is a library for scheduling background jobs in your Android app. It provides a JobScheduler-compatible API that works on all recent versions of Android (API level 9+). The only condition is that you must have Google Play services installed on the device to work with this API.

Problems with Background Services

Running services in the background is very expensive for the battery life of the device, which is especially harmful when they're not actually doing any work that's required for the user. That problem is increased when those background threads are listening for frequently sent broadcasts (like and android.hardware.action.NEW_PICTURE etc.). Even worse, there's no way of specifying constraints for these broadcasts. Listening for CONNECTIVITY_CHANGE broadcasts does not guarantee that the device has an active network connection, only that the connection was just changed.


The FirebaseJobDispatcher currently depends on the scheduling component in Google Play services. Because of that, it won't work on environments without Google Play services present.


Comparison to other libraries


Minimum API

Requires Google Play

Service API1

Custom retry strategies

Framework JobScheduler





Firebase JobDispatcher










Setup the Dispatcher

Let’s start by setting up the app in our build.gradle. There are options for this. Either add 2 dependencies as shown:


compile ''

compile 'com.firebase:firebase-jobdispatcher:0.5.0'

Otherwise add the following:

compile 'com.firebase:firebase-jobdispatcher-with-gcm-dep:0.5.0'


The simplest possible JobService:


import com.firebase.jobdispatcher.JobParameters;
import com.firebase.jobdispatcher.JobService;

public class MyJobService extends JobService {
   public boolean onStartJob(JobParameters job) {
       // Do some work here

       return false; // Answers the question: "Is there still work going on?"

   public boolean onStopJob(JobParameters job) {
       return false; // Answers the question: "Should this job be retried?"

Adding it to the manifest

       <action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>"

Creating a Dispatcher

// Create a new dispatcher using the Google Play driver.
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));

Scheduling a simple job

Job myJob = dispatcher.newJobBuilder()
   .setService(MyJobService.class) // the JobService that will be called
   .setTag("discover-sdk-tag")        // uniquely identifies the job


Scheduling a more complex job

Bundle myExtrasBundle = new Bundle();
myExtrasBundle.putString("some_key", "some_value");

Job myJob = dispatcher.newJobBuilder()
   // the JobService that will be called
   // uniquely identifies the job
   // one-off job
   // don't persist past a device reboot
   // start between 0 and 90 seconds from now
   .setTrigger(Trigger.executionWindow(0, 90))
   // don't overwrite an existing job with the same tag
   // retry with exponential backoff
   // constraints that need to be satisfied for the job to run
       // only run on an unmetered network
       // only run when the device is charging


Cancelling a job


Cancelling all jobs



Firebase JobDispatcher vs JobScheduler vs GcmTaskService


Firebase Android JobDispatcher is a layer of abstraction around job scheduling engines on Android. And for now they only have one driver implementation for the GCM Network Manager. That means currently it behaves the same way that the GCM Network Manager behaves. Hopefully in the future more Drivers will be implemented.


There is at least one drawback to using JobScheduler. As of the writing of this post, it’s compatible only with an API level of 21 or higher, so check into the distribution of Android devices running various API levels. While there is technically no backport of JobScheduler, a similar tool is GCM Network Manager.


GCM Network Manager is essentially like Lollipop's JobScheduler, except it's available for older releases as well.


GCM (Google Cloud Messaging) Network Manager actually uses JobScheduler behind the scenes for API level 21 and higher, and features support for previous versions of Android with Google Play Services.


>>> From API level 21 forward, GCM Network Manager uses the framework’s JobScheduler.


It’s important to note when comparing GCM Network Manager to JobScheduler that application and Google Play Service updates will wipe out scheduled Services. The onInitializeTask()method of GcmTaskService can be overridden to be notified of this event and respond accordingly to reschedule the service.


Also, be aware that GCM Network Manager and JobScheduler were not built to perform tasks immediately or at a given time.

Additional: Sync Adapter

Sync Adapters were introduced with the intent of keeping Android devices and servers synchronised. Syncs could be triggered from data changes in either the server or device, or by elapsed time and time of day (pretty cool, huh). The system will try to batch outgoing syncs to preserve battery life, and transfers that are unable to run will queue up for transfer at some later time. The system will attempt syncs only when the device is connected to a network (that’s being pretty smart).

These concepts should sound pretty familiar by now. A nice optional feature included with Sync Adapters is the ability for users to see information about syncs and the ability to turn off syncing all together.


Sync Adapters often require quite a bit more work to set up than the previous methods we discussed. However, if an application already supports android users and uses Content Providers, a lot of the boilerplate work is already done.


Additional: Doze

Doze Mode was introduced in Android 6.0 as a way to optimise battery power while a user is away from their device for some time. Doze Mode is automatically enabled on devices running Android API level 23 and higher. It is not a scheduler that developers need to implement.


>>> Doze affects the other schedulers we have already discussed.


Doze Mode is enabled after a period of time under these conditions: the user’s screen has been off, the device has been stationary (no movements at all) and the device is not plugged for charging. When in Doze Mode, network access is suspended, the system ignores wake locks and standard alarms are deferred, including the running of Sync Adapters and JobSchedulers. There are maintenance windows that infrequently occur to allow all of these schedulers within the system to run at once, therefore preserving battery through infrequent and grouped network calls.


In this article, we’ve covered how to use the Firebase JobDispatcher API which is compatible with the JobScheduler API to schedule background jobs in your Android app. We also compared GCM Network Manager, JobScheduler and the Firebase JobDispatcher. Now you’re free to build better apps that will work excellently in the background as well as perform magical tasks all while remaining hidden.

By Shubham Aggarwal | 2/18/2017 | General

{{CommentsModel.TotalCount}} Comments

Your Comment


Recent Stories

Top DiscoverSDK Experts

User photo
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
User photo
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
Show All

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.


Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now