I currently am working on an update of an app, which has the following work flow:
- we upload a record to Firestore Database with some data
- This, through google cloud, automatically generates a push notification, which is sent through a channel to our users' devices.
- The devices receive this notification, and with the
onMessageReceivedfunction from FCM, we also trigger a sync with the Firestore Database. - The attempt to sync with the Firestore Database is made.
- Regardless on whether this sync was correct, or not, the push notification is still shown to the user. When he presses on this, the app crashes.
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!