Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase job dispatcher is not executing when app is closed

I have setup a job through firebase job dispatcher, which should execute after X-hours. It works fine when app is open or in background but not working when app is closed.

Too many people have faced this issue, even such issues are still open in Github, but I didn't find solution.

Here is my code:

public void setupJob(){
    FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
    final int periodicity = (int) TimeUnit.HOURS.toSeconds(3);
    final int toleranceInterval = 30;

    Job myJob = dispatcher.newJobBuilder()
            .setService(MyScheduler.class)
            .setTag("MY_JOB_TAG")
            .setRecurring(false)
            .setLifetime(Lifetime.FOREVER)
            .setTrigger(Trigger.executionWindow(periodicity, periodicity + toleranceInterval))
            .setReplaceCurrent(false)
            .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
            .build();

    dispatcher.mustSchedule(myJob);
}

Making .setRecurring(true) fires it but only once you open the app and then it repeats and works fine even if the app is closed.

Help please!

like image 223
Zeero0 Avatar asked Nov 07 '22 21:11

Zeero0


1 Answers

Which version of Android are you using for testing?

Android Marshmallow introduced new power-saving features. One of these features is Doze which reduces battery consumption by deferring background CPU and network activity for apps when the device is unused for long periods of time. The second one is App Standby which defers background network activity for apps with which the user has not recently interacted.

See:
https://developer.android.com/training/monitoring-device-state/doze-standby.html

Should also be noted that some custom Android releases (for example MIUI from Xiaomi, EMUI from Huawei) have additional permissions to allow running applications in background or receiving notifications (for example from Firebase Cloud Messaging).

like image 167
mrg Avatar answered Nov 15 '22 07:11

mrg