0

I have a firebase job dispatcher which is scheduled to run whenever network changes ,the job is working perfectly on a marshmallow device (23 API level) but the same code when run is not scheduling jobs on a oreo device(26 API level)

Here is my Job Service code:

public class MyJobService extends JobService {


@Override
public boolean onStartJob(JobParameters job) {

    Log.d("Executing","Job");
    Realm realm = Realm.getDefaultInstance();

    RealmResults<JsontoSend> realmResults = realm.where(JsontoSend.class).findAll();
   if(!realmResults.isEmpty()) {
        for (JsontoSend jsontoSend : realmResults) {
            final Intent intent = new Intent(getApplicationContext(), PostUploadIntentService.class);
            intent.putExtra("object", jsontoSend.getJson());
            new Thread(new Runnable() {
                @Override
                public void run() {
                    startService(intent);
                }
            }).run();
        }
    }


    return true;
}

@Override
public boolean onStopJob(JobParameters job) {
        Log.d("instopjob","cancelled");
        return true;

    }
}

This is my code where i have created the job :

 synchronized  public static void schedule(@NonNull final Context context){
    if(sInitialized)
        return;

    Driver driver=new GooglePlayDriver(context);
    FirebaseJobDispatcher dispatcher=new FirebaseJobDispatcher(driver);

    Job myJob = dispatcher.newJobBuilder()
            .setService(MyJobService.class)
            .setTag(JOB_TAG)
            .setRecurring(true)
            .setTrigger(Trigger.executionWindow(5, 60))
            .setLifetime(Lifetime.FOREVER)
            .setConstraints(Constraint.ON_ANY_NETWORK)
            .setReplaceCurrent(false)
            .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
            .build();

    dispatcher.schedule(myJob);
    sInitialized=true;

}

What i am trying to do is if i do not have internet connection then storing the data in local database and then running a job whenever i connect to internet and sync the data with server .The above code is working perfectly on marshmallow device but the job is never scheduled on oreo devices.

shashank chandak
  • 544
  • 5
  • 13
  • Sorry man, I have no idea why it doesn't work. But why do you use the firebase scheduler? Do you target on devices before 5.0 ? – Flavio Nov 12 '18 at 18:04
  • @Flavio can you give me some other approach that works on oreo devices for the same(i mean sync data to server whenever internet is connected). – shashank chandak Nov 12 '18 at 18:06

1 Answers1

0

well the problem here is probably because you use GooglePlayDriver which relies on google play services to be presented on the devices. If the services are not available the job will not be scheduled. So if you target on devices higher than 5.0 (which is true in most cases) you have to use android's JobScheduler build in since lollipop.

Flavio
  • 6,205
  • 4
  • 30
  • 28