Fix Background Uploads on Android#286
Conversation
parveshneedhoo
left a comment
There was a problem hiding this comment.
Tested on different devices (2 Pixels and 2 Samsung phones).
The behavior is inconsistent: sometimes the upload starts in the background, and sometimes it doesn’t
Should we check this further to understand why it’s not consistent?
f4fdb54 to
3d774b3
Compare
Samsung’s "Adaptive Battery" often restricts background tasks. or Go to Settings > Battery and device care > Battery. |
This PR fixes an issue where background uploads were being terminated by the OS when the app was killed on Android 12+ devices. The fix ensures the UploadTask is correctly promoted to a Foreground Service, allowing it to persist independently of the app process.
On Android 12+, WorkManager requires Expedited work requests to implement getForegroundInfo() so the system can display a notification if the task runs long. The plugin was missing this implementation, causing the system to deny the expedited execution or kill the worker when the app process died. Additionally, the worker was not explicitly calling setForegroundAsync for Android 12+, relying on default behavior which proved unreliable for app-kill scenarios.
Solution:
Implemented getForegroundInfo(): Overrode this method in UploadTask.java to return a valid ForegroundInfo object containing the upload notification.