How to start task periodically using firebase job

2019-09-17 00:13发布

问题:

I used Jobschuler for sending notification every x minutes(determined dynamically) like this

ComponentName componentName = new ComponentName(context,ClsJobService.class);

    JobInfo.Builder builder =  new JobInfo.Builder(0, componentName)
      .setPeriodic(duration * 60 * 1000);  //setting in millisecond
    JobScheduler jobScheduler =  (JobScheduler) context.getSystemService (Context.JOB_SCHEDULER_SERVICE);
    jobScheduler.schedule(builder.build());

and its working great it sends notification on time defined in JobService

now how can I convert it into firebase job scheduler, I did like this

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

    Job job = dispatcher.newJobBuilder()
            .setService(ClsJobService.class)
            .setTag("notification")        // uniquely identifies the job
            .setTrigger(Trigger.executionWindow(0, duration * 60))
            .setLifetime(Lifetime.FOREVER)
            .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
            .setRecurring(true)
            .build();


    dispatcher.mustSchedule(job);

but I sends notification only once when schedules, and after that not

回答1:

My best guess is that you should change setTrigger() to:

.setTrigger(Trigger.executionWindow(duration * 60, duration * 60))

EDITED to fix executionWindow