'FCM + Workmanager
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!
Solution 1:[1]
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:
- You have a little time span after receiving the push so the app will fall back asleep shortly afterward(this is how push notifications work in general).
- Also in general this WAS a place where you should create a Service, not to be killed, but with the new restrictions for starting foreground Services from the background - it is harder to do so: https://developer.android.com/about/versions/12/behavior-changes-12#foreground-service-launch-restrictions . Basically, Google is forcing us towards WM.
- If you set the priority to high, but the user does not click the notification and just dismisses it - it will be deprioritized and will no longer work for you.
- If you have a low priority basically the only option for you is to start a work from the push and when the Constraints are met - you do your work.
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:
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:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
