react-native-background-fetch: Unable to make a fetch when app is running in background

I have configured a background task in android and i want it to be triggered every 15mins irrespective of the app state. im using both configure and registerHeadlessTask

BackgroundFetch.configure({
     minimumFetchInterval: 15, // <-- minutes (15 is minimum allowed)
     stopOnTerminate: false,   // <-- Android-only,
     enableHeadless: true         // <-- Android-only
   },task, (error) => {
     console.log("[js] RNBackgroundFetch failed to start ::::", err);
   });

BackgroundFetch.registerHeadlessTask(task);

Your Environment

  • Plugin version: 2.5.6
  • Platform: Android
  • React Native version (react-native -v): 0.57.7

Expected Behavior

background task should work when the app is running in background or even when it is killed

Actual Behavior

the background task is working when the app is not running, but when the app is in running in the background, the task is triggered but then fetch is not happening and I get the response when I open the app

Context

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 16 (6 by maintainers)

Most upvoted comments

Hi @christocracy, we do have the same problem like @gghnisan. The headlessjs task does not run if the app is killed.

Some logs:

APP START:

06-23 18:53:37.708 29599 29599 D ReactNative: ReactInstanceManager.ctor()
06-23 18:53:37.710 29599 29599 D ReactNative: ReactInstanceManager.createReactContextInBackground()
06-23 18:53:37.710 29599 29599 D ReactNative: ReactInstanceManager.recreateReactContextInBackgroundInner()
06-23 18:53:37.710 29599 29599 D ReactNative: ReactInstanceManager.recreateReactContextInBackgroundFromBundleLoader()
06-23 18:53:37.710 29599 29599 D ReactNative: ReactInstanceManager.recreateReactContextInBackground()
06-23 18:53:37.710 29599 29599 D ReactNative: ReactInstanceManager.runCreateReactContextOnNewThread()
06-23 18:53:37.749 29599 29617 D ReactNative: ReactInstanceManager.createReactContext()
06-23 18:53:37.769 29599 29617 D ReactNative: Initializing React Xplat Bridge.
06-23 18:53:37.771 29599 29617 D ReactNative: Initializing React Xplat Bridge before initializeBridge
06-23 18:53:37.783 29599 29617 D ReactNative: Initializing React Xplat Bridge after initializeBridge
06-23 18:53:37.783 29599 29617 D ReactNative: CatalystInstanceImpl.runJSBundle()
06-23 18:53:37.790 29599 29622 D ReactNative: ReactInstanceManager.setupReactContext()
06-23 18:53:37.790 29599 29622 D ReactNative: CatalystInstanceImpl.initialize()
06-23 18:53:37.793 29599 29622 D ReactNative: ReactInstanceManager.attachRootViewToInstance()
06-23 18:53:38.018 29599 29621 I ReactNativeJS: Running application "BackgroundFetchBug" with appParams: {"rootTag":1}. __DEV__ === false, development-level warning are OFF, performance optimizations are ON
06-23 18:53:38.060 29599 29622 D TSBackgroundFetch: - configure: {
06-23 18:53:38.060 29599 29622 D TSBackgroundFetch:   "minimumFetchInterval": 15,
06-23 18:53:38.060 29599 29622 D TSBackgroundFetch:   "stopOnTerminate": false,
06-23 18:53:38.060 29599 29622 D TSBackgroundFetch:   "requiredNetworkType": 0,
06-23 18:53:38.060 29599 29622 D TSBackgroundFetch:   "requiresBatteryNotLow": false,
06-23 18:53:38.060 29599 29622 D TSBackgroundFetch:   "requiresCharging": false,
06-23 18:53:38.060 29599 29622 D TSBackgroundFetch:   "requiresDeviceIdle": false,
06-23 18:53:38.060 29599 29622 D TSBackgroundFetch:   "requiresStorageNotLow": false,
06-23 18:53:38.060 29599 29622 D TSBackgroundFetch:   "startOnBoot": true,
06-23 18:53:38.060 29599 29622 D TSBackgroundFetch:   "forceReload": false,
06-23 18:53:38.060 29599 29622 D TSBackgroundFetch:   "jobService": "com.transistorsoft.rnbackgroundfetch.HeadlessJobService"
06-23 18:53:38.060 29599 29622 D TSBackgroundFetch: }
06-23 18:53:38.062 29599 29622 D TSBackgroundFetch: - start
06-23 18:53:38.068 29599 29621 I ReactNativeJS: BackgroundFetch is enabled

APP MOVED TO BACKGROUND, Running: adb shell cmd jobscheduler run -f com.backgroundfetchbug 999

06-23 18:53:51.348 29599 29599 D TSBackgroundFetch: - Background Fetch event received
06-23 18:53:51.354 29599 29621 I ReactNativeJS: [js] Received background-fetch event
06-23 18:53:51.356 29599 29622 D TSBackgroundFetch: - finish
06-23 18:53:51.356 29599 29622 D TSBackgroundFetch: - jobFinished

APP KILLED:

06-23 18:53:57.190 29599 29599 D ReactNative: ReactInstanceManager.detachViewFromInstance()

Running: adb shell cmd jobscheduler run -f com.backgroundfetchbug 999; NOTHING HAPPENS

Here is a repo with simple repro app. https://github.com/skaldo/BackgroundFetchBug

Device is Samsung Galaxy S8 running Android 9.

Could you please check if there’s something wrong with the code? Just run it with react-native run-android --variant=release, signing is configured. Thanks!