【问题标题】:How to start task periodically using firebase job scheduler如何使用 Firebase 作业调度程序定期启动任务
【发布时间】:2017-07-24 16:49:59
【问题描述】:

我使用 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);

但我只在安排时发送一次通知,之后不再发送通知

【问题讨论】:

  • 我没用过这个库,但是如果在调用 setTrigger() 之前调用 setRecurring() 会发生什么?
  • 查看不应该产生影响的代码...
  • 未测试,但其对象和顺序可能无关紧要

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


【解决方案1】:

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

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

编辑修复执行窗口

【讨论】:

  • 它会给出异常开始时间不应小于结束时间
  • 也试过了(持续时间 * 60,持续时间 * 60 + 40)
猜你喜欢
  • 2017-07-15
  • 1970-01-01
  • 2014-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-18
  • 2017-03-14
  • 1970-01-01
相关资源
最近更新 更多