13

I would like to know if it's possible to use Firebase jobdispatcher to schedule an url hit and get the response in order to update the db. I would like it to run once per day at night. Does anyone know if this is possible?

I can't find any good example of doing this. I already read android documentation and https://github.com/firebase/firebase-jobdispatcher-android#user-content-firebase-jobdispatcher- .

I need to use Firebase jobdispatcher because I'm targeting API 16.

Thanks in advance.

UPDATE

This is what I did to schedule it once per day.

final int periodicity = (int) TimeUnit.HOURS.toSeconds(24);
final int toleranceInterval = (int) TimeUnit.HOURS.toSeconds(1);

FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
Job job = dispatcher.newJobBuilder()
        .setService(UpdateTVJobService.class)
        .setTag(JOB_TAG)
        .setTrigger(Trigger.executionWindow(periodicity, periodicity + toleranceInterval))
        .setLifetime(Lifetime.FOREVER)
        .setRecurring(true)
        .setReplaceCurrent(true)
        .build();

int result = dispatcher.schedule(job);

if (result != FirebaseJobDispatcher.SCHEDULE_RESULT_SUCCESS) {
    Log.d("JOB_TAG", "ERROR ON SCHEDULE");
}
Sol
  • 833
  • 1
  • 9
  • 17

2 Answers2

16

You can schedule recurring jobs using Firebase JobDispatcher.As per your requirement,you need to create a service extending JobService that get response from url and update the db . Then you can schedule this service using Firebase JobDispatcher .In executionWindow you have to specify the earliest and latest time that job should run in ideal circumstances.

If you want to schedule job after every 24 hours you can use execution window (60*60*24,60*60*24+60).Then if you want that it should run every night then you have to make sure that it is initially scheduled at night.For that you can initially use AlarmManager to be fired at night(only once) when app is installed and schedule a recurring job using job dispatcher OR another way is that based on difference from now and the desired execution time you can schedule a non recursive job using jobdispatcher which will run at night and inside that job service you can schedule recurring job using job dispatcher .

ExecutionWindow specifies approximate time. It's not guaranteed it would run at the given window. If it misses the window the job will run at earliest time later under ideal circumstances.For recurring jobs once the job has finished next job will calculate execution window time from the time job last run.

Job myJob = dispatcher.newJobBuilder()
                    .setTag("my-unique-tag")
                    .setRecurring(true)
                    .setLifetime(Lifetime.FOREVER)
                    .setService(MyJobService.class)
                    .setTrigger(Trigger.executionWindow(60*60*24,60*60*24+60))
                    .setReplaceCurrent(false)
                    .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
                    .setConstraints(Constraint.ON_ANY_NETWORK)
                    .setExtras(myExtrasBundle)
                    .build();

dispatcher.mustSchedule(myJob);
Android Developer
  • 9,157
  • 18
  • 82
  • 139
  • Thanks, I done something similar. I will update my post for other people. – Sol Feb 10 '17 at 15:31
  • @Bhuvnesh Varma : need to ask regarding trigger window ? – young_08 Jun 26 '17 at 19:39
  • @young_08 please ask whatever your query is? – Android Developer Jun 27 '17 at 05:25
  • @BhuvneshVarma : Hi , thank you so much for replying. I really need some firebase job scheduler advice at this moment. I have initiated my scheduler from Application class. Now everything is working fine until app goes in background or kill. My scheduler doesn't run. Hence, the location service i am running from onStart() of scheduler didn't work too. Please help me with this. If u want i can post a question with code. – young_08 Jun 27 '17 at 05:32
  • @young_08 it will be better if you can post a question with the code you are using – Android Developer Jun 27 '17 at 05:40
  • @BhuvneshVarma Yes,surey .Here is link https://stackoverflow.com/questions/44772956/firebase-job-scheduler-not-working-in-background – young_08 Jun 27 '17 at 05:43
  • @BhuvneshVarma Can you go into more detail how to use AlarmManager in order to have the JobDispatcher run at a specific time of the day? Many thanks in advance :) – VollNoob Nov 29 '17 at 15:54
1

You can schedule a recurring job by telling the Job.Builder to create a recurring job with a Trigger that has an execution window according to your needs.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441