What I want to create: An offline app that sends a text notification to user from the database (Room) once every 5 days at a random time. I test the app on my real device Redmi Note 7 (MIUI).
So I tried to use Alarm Manager and Work Manager for repeating task but it only worked right (even app is killed) if I enabled autorun and disabled buttery optimization for my application. Then I found out that many manufacturers like Xiaomi, Oppo, Huawei and similar restrict background work for apps.
I have 2 questions:
- What should I use to write reliable recurring notifications (not with foreground service) on every android device without requesting users to enable autorun and similar app settings?
- How apps like Twitter and Telegram sends notifications without enabling extra settings, although they use the internet unlike my app
2
Answers
So, I found out that there is no way to send reliable recurring notifications offline via Alarm Manager or Work Manager on every android device. I use FCM and everything works great
Twitter and the like use FCM to send a high priority push notification. A process on the device which is sleeping waiting on input from a socket receives it and starts their app (that process is part of Android, they don’t write it).
You should be able to reliably do background processing on Android if its brief and occassional. WorkManager, JobScheduler, and AlarmManager would all work for that, with some better than others depending on your needs. I suggest you either edit or start a new question with the code you said wasn’t working and an accurate description of how often it needs to run.