react-native-background-fetch icon indicating copy to clipboard operation
react-native-background-fetch copied to clipboard

Need to trigger long-running background task with help of foreground service(Notification)

Open pratikSeven opened this issue 9 months ago • 3 comments

Your Environment

  • Plugin version: 4.2.4
  • Platform: Android
  • OS version: 14
  • Device manufacturer / model: Pixel 6a
  • React Native version (react-native -v): 0.73.2
  • Plugin config

Expected Behavior

I am able to trigger the background task successful and it works great. I want to start long-running background task for uploading file to server in backend and show progress of upload in foreground notification (using notifee )

Actual Behavior

Background task starts but automatically gets paused as it requires foreground notification to keep long task running but i get error when i try to start startForegroundService() not allowed due to mAllowStartForeground false

Steps to Reproduce

  1. configs BackgroundFetch.configure( { minimumFetchInterval: 15, stopOnTerminate: false, startOnBoot: true, enableHeadless: true, forceAlarmManager: true, },

Context

Want to start long-running background task for uploading file to server in backend and show progress of upload in foreground notification. Can you please help how can i achieve this i have tried multiple way to do it but i get error as i mentioned above.

Debug logs

pratikSeven avatar May 02 '24 11:05 pratikSeven

Need to trigger long-running background task with help of foreground service

Are you aware of Foreground Service Launch Restrictions introduced with Android 12?

See the list of Exemptions from background-service starts, which includes an exemption for services triggered with "exact alarms", which requires the now-restricted USE_EXACT_ALARM permission. The Play Store will ask you to justify your usage of this permission.

christocracy avatar May 02 '24 14:05 christocracy

Thank you for the response. Yes i have added the permission and also i have adde the param forceAlarmManager: true It starts my uploading task but only for few seconds and then it gets paused. If i open the app task resumes otherwise it stays paused. Is this expected behaviour ?

pratikSeven avatar May 06 '24 06:05 pratikSeven

I tested uploading a 15MB file in my Fetch callback. I see no issues.

There is no need for a foreground-service. Android allows your app to stay alive in the background until you call BackgroundFetch.finish(taskId). This is the entire purpose of this plugin -- to allow your app to run periodic backround-tasks until you signal to the OS that you're finished by calling .finish(taskId).

christocracy avatar May 07 '24 15:05 christocracy

Yes you are correct it is working. Just that i wanted to trigger a foreground service with notification and it was throwing an error but now i have fixed that with native custom module. Thank you for the response.

pratikSeven avatar May 20 '24 05:05 pratikSeven