如何使用firebase作业调度程序定期启动任务

时间:2017-07-24 16:50:00

标签: java android android-notifications android-jobscheduler firebase-job-dispatcher

我使用Jobschuler每隔x分钟发送一次通知(动态确定),就像这样

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());

并且工作得很好,它会按照JobService中定义的时间发送通知

现在如何将其转换为firebase作业调度程序,我确实喜欢这个

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

但是我只在计划时发送一次通知,之后不发送

1 个答案:

答案 0 :(得分:0)

我最好的猜测是你应该将setTrigger()更改为:

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

已修改以修复executionWindow