Similar question: Android workmanager scheduled worker lost after task killed
The answer here suggests that this is a problem with specific manufacturers, but I am using an Android One phone which has the unmodified OS.
For the most part my PeriodicWorkRequest works as expected, but then for a straight 5-8 hours window, it doesn't get executed at all. I log whenever the request is executed in a log file, and this is how it looks:
As visible in the image, the red box indicates the interval where WorkManager did not run the request. I can assure that the app was never force killed since install. It was cleared from recents some times, but that did not seem to have any immediate effects.
This is how I initialized the request:
PeriodicWorkRequest checkLastSeenRequest =
new PeriodicWorkRequest.Builder(LastSeenWorker.class,
PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS, TimeUnit.MILLISECONDS,
PeriodicWorkRequest.MIN_PERIODIC_FLEX_MILLIS, TimeUnit.MILLISECONDS)
.setBackoffCriteria(
BackoffPolicy.LINEAR,
PeriodicWorkRequest.MIN_BACKOFF_MILLIS,
TimeUnit.MILLISECONDS)
.build();
WorkManager.getInstance(this).enqueueUniquePeriodicWork(
"checkLastSeen",
ExistingPeriodicWorkPolicy.KEEP,
checkLastSeenRequest);
And this is the createWork() method of RxWorker:
@NonNull
@Override
public Single<Result> createWork() {
return Single.create(subscriber -> {
FirebaseFirestore firestore = FirebaseFirestore.getInstance();
firestore.collection("users").document("doc").get()
.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
DocumentSnapshot doc = task.getResult();
if (doc != null && doc.exists() && doc.getData() != null
&& doc.getData().get("lastSeen") != null) {
--> // this is where I log
subscriber.onSuccess(Result.success());
} else {
--> XLog.e("No document found to read from");
subscriber.onError(new Error("Document not found"));
}
} else {
--> XLog.e(task.getException());
subscriber.onError(task.getException());
}
});
});
}
From the documentation here my understanding is that in doze mode as well there is a window where all the requests are allowed to execute even as these become less frequent. So why is there a complete blackout for more than 6 hours? Do I need to get battery optimization permissions for it to work as expected? Any help appreciated!
