androidfirebasefirebase-cloud-messagingfirebase-job-dispatcherjobservice

Firebase job service started with big delay


I am testing an application that receives Firebase cloud data messages and processes them in the Firebase job service. Receiving messages in the FirebaseMessagingService occurs instantly and without problems, but the Firebase job service sometimes starts with a long delay (5-10 minutes), and sometimes it does not start at all. The dispatcher.schedule (myJob) method always gives the result SCHEDULE_RESULT_SUCCESS. This is the job scheduling functionality:

// For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
Bundle bundle = new Bundle();
bundle.putInt(WatchDogService.REQUEST_ID, request.ordinal());
bundle.putString(REQUEST_PARAM, parameter);
FirebaseJobDispatcher dispatcher =
        new FirebaseJobDispatcher(new GooglePlayDriver(this));
Job myJob = dispatcher.newJobBuilder()
        .setService(LongJobService.class)
        .setTag("ua.ers.LongJobService")
        .setTrigger(Trigger.NOW)
        .setReplaceCurrent(true)
        .setExtras(bundle)
        .build();
int result = dispatcher.schedule(myJob);
Log.d(TAG, "Schedule result: " + result);

Here is a Firebase Job Service class:

public class LongJobService extends JobService {
    private static final String TAG = "LongJobService";

    @Override
    public boolean onStartJob(final JobParameters jobParameters) {
        Log.d(TAG, "LongJobService started");    
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters jobParameters) {
        Log.d(TAG, "LongJobService stopped");
        return false;
    }
}

Tell me, please, what could be the cause of the problem?


Solution

  • According to documentation, this is just the usual behaviour

    The scheduler backend is encouraged to use the windowEnd value as a signal that the job should be run, but this is not an enforced behavior.

    But for a better execution try

    Trigger.executionWindow(0, 0)
    

    instead of

    Trigger.NOW
    

    So it will look like

    Job myJob = dispatcher.newJobBuilder()
    .setService(Service.class)
    .setRecurring(true) // if task is periodic, else pass "false"
    .setTrigger(Trigger.executionWindow(0, 0))
    .setTag("tag")
    .build();