androidalarmmanagerandroid-jobschedulerfirebase-job-dispatcher

Schedule a task weekly using Firebase job dispatcher


Have tried job dispatcher for scheduling repetitive tasks on Hourly basis. Have written a code snippet for the problem but not sure if this is a correct implementation or not.

Snippet is for scheduling a task at 11 Hour of Monday at every week.

Any correction on this or other possible solution will help a lot.

    Calendar c1 = Calendar.getInstance();
    c1.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    c1.set(Calendar.HOUR_OF_DAY, 11);
    c1.set(Calendar.MINUTE, 0);
    c1.set(Calendar.SECOND, 0);
    c1.set(Calendar.MILLISECOND, 0);

    FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(Splash_onboarding.this));

    Job myJob = dispatcher.newJobBuilder()
            .setService(ScheduledNotificationService.class)
            .setTag(dispatcherTag)
            .setRecurring(true)
            .setLifetime(Lifetime.FOREVER)
            .setTrigger(Trigger.executionWindow(Math.round(c1.getTime().getTime() / 1000), (Math.round(c1.getTime().getTime() / 1000)) + 60))
            .setReplaceCurrent(true)
            .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
            .build();

    dispatcher.mustSchedule(myJob);

Solution

  • I don't think you are implementing Firebase JobDispatcher correctly

    Trigger.executionWindow()
    

    In this, you write after how much time your job executes itself after scheduling your job. For more info see this : https://stackoverflow.com/a/42111723/7384780

    You can solve your problem by scheduling your first non-recurring job with executionWindow (get time of next monday) - System.currentTimeMillis() and then starting a recurring job inside JobService with executionWindow of 7 * 24 * 60 * 60.