I currently am working on an update of an app, which has the following work flow:
onMessageReceived function from FCM, we also trigger a sync with the Firestore Database.I have the theory though that when Android enters Doze mode, step 4 gives an error; so the device does not sync with the Firestore Database, and hence the app will crash for the user. This is happening to around 20% of our users currently.
I then discovered about Workmanager, which I believe can help solve this issue. Nevertheless, I'm pretty new to this. I was thinking that one solution can be: I specifically create a WorkRequest with a constraint stating that the device should be connected to the internet. In this WorkRequest I'd perform this database sync as well as showing the notification to the user.
Nevertheless, this can cause some delays, as it depends on when the user will connect their device to the internet. Ideally, I believe the best solution would be something like described here. In the end, in my case, the syncing (scheduled job) is triggered by an external event (FCM), but I'm having trouble to understand whether this really is the best possible solution.
What do you think? Would the first solution be good? If the second is better, do you maybe have an example on how the code would look like (a skeleton basically, so I can understand how to relate WorkManager with FCM)? Or is Workmanager maybe not the best solution for this?
Many thanks!
Read here:
https://developer.android.com/topic/performance/appstandby
And here:
https://firebase.google.com/docs/cloud-messaging/concept-options#setting-the-priority-of-a-message
But long story short - if you use push - you need to give high priority to the messages so the user will interact with them and this will move the app to the active bucket. This can work even in doze mode.
But if there is no user interaction:
So basically this is my advice:
Send the push with low priority. You do not need the user.
Create work with the right constraints to do the job. ( you can put some settings in the application for wi-fi or not, high/low battery, charging or not, etc) https://developer.android.com/topic/libraries/architecture/workmanager/how-to/define-work#work-constraints
When the work is started you can show a notification(it is part of the API - ForegroundInfo) for active syncing and when the work finishes - you can show some notification for finished syncing.
Also, have in mind that there might be unfinished work when you receive the next push. So look for chaining works and also unique works.
For example, you might want to have only one work and with unique - you can replace the old one or even append the new to it. Check here:
https://developer.android.com/topic/libraries/architecture/workmanager/how-to/managing-work#conflict-resolution
I would advise you to watch this playlist. In the end, there is also a Q&A with the guys behind the WM. You will understand most of the stuff you will need:
https://www.youtube.com/watch?v=NtpgWjiXEfg&list=PLWz5rJ2EKKc_J88-h0PhCO_aV0HIAs9Qk&ab_channel=AndroidDevelopers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With