I'm developing an Android (Kotlin) application where administrators can register dependents and monitor their position and device status all the time. Also, these administrators can mark places on a map where they wish to get notified whenever their dependents enter or exit these locations. To accomplish this, I'm using geofences for the location enter/exit monitoring, and a unique periodic work that repeats from 10-20 minutes.
What this worker does:
- Tries to fetch user location (timeout:30 sec) and uploads it to the server.
- Checks the server for profile updates (name, email, other infos), updating the local database (Room) if needed.
- Checks the server for new places. If new places are found, save them on the local database (Room), delete all current geofences and recreate them. (The geofences are NEVER_EXPIRE)
I have also added a broadcast receiver for BOOT_COMPLETED events, responsible for recreating the geofences from the places saved locally by the worker, as geofences are not recreated by the system after a restart. The worker is automatically restarted by the system.
To help debug this, I've created a simple Log system using Room that logs every time the Worker is executed, when geofences are created/deleted, events like BOOT_COMPLETED are received and geofence transitions happen.
The problem I have is that this Worker stops repeating itself, usually around 3 days. If the server notices that a dependent has not sent any updates in an hour, it fires a "Revive" push notification. The app uses this to restart the Periodic Worker. Even though the app received and restarts the work successfully, it only repeats for about 1 hour and then stops again.
At the same time, the geofence transitions stop triggering. I couldn't find any relation between the geofence triggers and the periodic worker, other than being responsible for creating the geofences if new places are found on the server. From analyzing the logs, the geofences were correctly created after a device reboot, but they do not trigger.
If the user opens the app, the worker is recreated, goes back to repeating for the 2-3 days interval, and the geofences start triggering correctly.
Has anyone faced a similar problem with Workers or geofences and managed to come up with a solution?