question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to make a fetch when app is running in background

See original GitHub issue

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

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
skaldocommented, Jun 23, 2019

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!

0reactions
stale[bot]commented, Aug 29, 2019

Closing this issue after a prolonged period of inactivity. Fell free to reopen this issue, if this still affecting you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to ensure Background Fetch is enabled in my app
Hi all as titled, I am creating an app (Lets give this app name as 'TM'). TM is a GPS Location Navigatator app...
Read more >
Error fetch when App is put in background - Stack Overflow
Recently fell into this issue. Sometimes network request fails on iOS when app is in background. https://developer.apple.com/forums/thread/ ...
Read more >
react-native-background-fetch - npm
Background Fetch is a very simple plugin which attempts to awaken an app in the background about every 15 minutes, providing a short...
Read more >
How to run code when your app is terminated
To try this out, hold down the Alt key then go to the Product menu and choose “Run…”. This will let you edit...
Read more >
background_fetch | Flutter Package - Pub.dev
Background Fetch is a very simple plugin which will awaken an app in the background about every 15 minutes, providing a short period...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found