1

I looked at several answers for this but am not getting anywhere. Does anyone know why my FirebaseDispatcher is not executing? I set a trigger window between 1 and 5 seconds just to see if it works. My application launches and this method is called upon from a utility class:

private static void scheduleFirebaseJobSync(@NonNull final Context context){

    try {
        com.firebase.jobdispatcher.Driver driver = new GooglePlayDriver(context);
        FirebaseJobDispatcher firebaseJobDispatcher = new FirebaseJobDispatcher(driver);

        Job syncJob = firebaseJobDispatcher.newJobBuilder()
                .setService(FireBaseJobService.class)
                .setTag(WEATHER_SYNC_TAG)
                .setConstraints(Constraint.ON_ANY_NETWORK)
                .setLifetime(Lifetime.FOREVER)
                .setRecurring(true)
                .setTrigger(Trigger.executionWindow(1,5))
                .setReplaceCurrent(true)
                .build();
        firebaseJobDispatcher.schedule(syncJob);
    }

Here is the FirebaseJobService Class that is called:

public class FireBaseJobService extends JobService {
private AsyncTask<Void,Void,Void> fetchMovieDate;

@Override
public boolean onStartJob(final JobParameters job) {
    fetchMovieDate = new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {

            Context context = getApplicationContext();
            WeatherSyncTask.syncWeatherDB(context);
            jobFinished(job,false);
            Log.d("FIREBASEJOB", "IN FIRE BASE JOB FINISHED");
            return null;
        }
        @Override
        protected void onPostExecute(Void aVoid) {
            jobFinished(job,false);
        }
    };
    fetchMovieDate.execute();
    return true;
}

@Override
public boolean onStopJob(JobParameters job) {
    if (fetchMovieDate!=null){
        fetchMovieDate.cancel(true);
    }

    return true;
}}
Mark F
  • 1,523
  • 3
  • 23
  • 41

2 Answers2

0

Register Permissions in AndroidManifest.xml:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Add Service declaration in AndroidManifest.xml:

<service
    android:exported="false"
    android:name=".FireBaseJobService ">
    <intent-filter>
        <action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
    </intent-filter>
</service>

Dispatch the Job with one of this two options:

firebaseJobDispatcher.schedule(syncJob);
firebaseJobDispatcher.mustSchedule(syncJob);

And stuff in the JobService is:

public class FireBaseJobService extends JobService {

    @Override
    public boolean onStartJob(final JobParameters job) {
        WeatherSyncTask.syncWeatherDB(getApplicationContext());
        return false;
    }

    @Override
    public boolean onStopJob(JobParameters job) {
        return true;
    }}
}
jantursky
  • 1,122
  • 1
  • 10
  • 21
0

In addition to all the other answers below, don't forget to use a more recent firebase-jobdispatcher lib.

In my case a similar problem with same code structure in the request and after implemented the suggestions of jantursky below, it was solved by changing the version of firebase lib imported in build.gradle to a recent one.

I had : implementation 'com.firebase:firebase-jobdispatcher:0.5.0'

and I then I've put: implementation 'com.firebase:firebase-jobdispatcher:0.8.5'

probably a more recent one will be good the same.

android_dev71
  • 517
  • 5
  • 7