androidandroid-servicefirebase-job-dispatcher

Firebase Job Dispatcher Not Scheduling


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;
}}

Solution

  • 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;
        }}
    }