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.

Not able to run headless task in background.[Android-9]

See original GitHub issue

Your Environment

  • Plugin version: 2.7.0
  • Platform: Android
  • OS version: windows 10 (develop)/ Android-9(test device)
  • Device manufacturer / model: redmi poco f1
  • React Native version (react-native -v): react-native-cli: 2.0.1 react-native: 0.60.5
  • Plugin config

Expected Behavior

Execute(hit api) periodically after 15 min

Actual Behavior

After closing the app, task is listed when checked via command adb shell dumpsys jobscheduler | grep com.bg_fetch

JOB #u0a478/999: 8b2dc7 com.bg_fetch/com.transistorsoft.tsbackgroundfetch.FetchJobService u0a478 tag=job/com.bg_fetch/com.transistorsoft.tsbackgroundfetch.FetchJobService Source: uid=u0a478 user=0 pkg=com.bg_fetch Service: com.bg_fetch/com.transistorsoft.tsbackgroundfetch.FetchJobService #u0a478/999 from u0a478 idle: com.bg_fetch [RUN_ANY_IN_BACKGROUND allowed] RUNNABLE #u0a478/999 from u0a478: com.bg_fetch RUNNABLE

After running manual event, adb shell cmd jobscheduler run -f com.bg_fetch 999 output-Running job [FORCED] But the task is not complted(api called) And job is killed there is no job when again checked with
adb shell dumpsys jobscheduler | grep com.bg_fetch No output

Steps to Reproduce

1.index .js

import BackgroundFetch from "react-native-background-fetch";

let MyHeadlessTask = async () => {
    console.log('[BackgroundFetch HeadlessTask] start');
    let response = await fetch('http://431c21b5.ngrok.io/');
    let responseJson = await response.json();
    console.log('[BackgroundFetch HeadlessTask] response: ', responseJson);
    BackgroundFetch.finish();
  }

  BackgroundFetch.registerHeadlessTask(MyHeadlessTask);

2.App.js

componentDidMount() {
    // Configure it.
    BackgroundFetch.configure({
      minimumFetchInterval: 15,     // <-- minutes (15 is minimum allowed)
      // Android options
      stopOnTerminate: false,
      startOnBoot: true,
      requiredNetworkType: BackgroundFetch.NETWORK_TYPE_ANY, // Default
      requiresCharging: false,      // Default
      requiresDeviceIdle: false,    // Default
      requiresBatteryNotLow: false, // Default
      requiresStorageNotLow: false,  // Default
      enableHeadless:true,
    }, async() => {
      console.log('[BackgroundFetch HeadlessTask] start');
      let response = await fetch('http://431c21b5.ngrok.io/');
      let responseJson = await response.json();
      console.log('[BackgroundFetch HeadlessTask] response: ', responseJson);
      BackgroundFetch.finish(BackgroundFetch.FETCH_RESULT_NEW_DATA);
    }, (error) => {
      console.log("[js] RNBackgroundFetch failed to start");
    });

    // Optional: Query the authorization status.
    BackgroundFetch.status((status) => {
      switch(status) {
        case BackgroundFetch.STATUS_RESTRICTED:
          console.log("BackgroundFetch restricted");
          break;
        case BackgroundFetch.STATUS_DENIED:
          console.log("BackgroundFetch denied");
          break;
        case BackgroundFetch.STATUS_AVAILABLE:
          console.log("BackgroundFetch is enabled");
          break;
      }
    });
  }

Context

Want to send data to backend for processing

Debug logs

09-05 17:36:07.244 24121 24155 D TSBackgroundFetch: - configure: { 09-05 17:36:07.244 24121 24155 D TSBackgroundFetch: “minimumFetchInterval”: 15, 09-05 17:36:07.244 24121 24155 D TSBackgroundFetch: “stopOnTerminate”: false, 09-05 17:36:07.244 24121 24155 D TSBackgroundFetch: “requiredNetworkType”: 1, 09-05 17:36:07.244 24121 24155 D TSBackgroundFetch: “requiresBatteryNotLow”: false, 09-05 17:36:07.244 24121 24155 D TSBackgroundFetch: “requiresCharging”: false, 09-05 17:36:07.244 24121 24155 D TSBackgroundFetch: “requiresDeviceIdle”: false, 09-05 17:36:07.244 24121 24155 D TSBackgroundFetch: “requiresStorageNotLow”: false, 09-05 17:36:07.244 24121 24155 D TSBackgroundFetch: “startOnBoot”: true, 09-05 17:36:07.244 24121 24155 D TSBackgroundFetch: “forceReload”: false, 09-05 17:36:07.244 24121 24155 D TSBackgroundFetch: “jobService”: “com.transistorsoft.rnbackgroundfetch.HeadlessJobService” 09-05 17:36:07.244 24121 24155 D TSBackgroundFetch: } 09-05 17:36:07.247 24121 24155 D TSBackgroundFetch: - start

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
christocracycommented, Sep 6, 2019

In my BackgroundFetch headless-task, I’ve added some code similar to yours, executing a request to my dev server to retrieve a list of devices as JSON:

http://tracker.transistorsoft.com/devices?company_token=transistor-rn

📂 index.js

let BackgroundFetchHeadlessTask = async (event) => {
  console.log('[BackgroundFetch HeadlessTask] start');

  let url = "http://tracker.transistorsoft.com/devices?company_token=transistor-rn";
  let response = await fetch(url);
  let json = await response.json();
  console.log('[BackgroundFetch] response: ', json);

  console.log('[BackgroundFetch HeadlessTask] finished');

  BackgroundFetch.finish();
}

BackgroundFetch.registerHeadlessTask(BackgroundFetchHeadlessTask);

  • After terminating my app, I simulate a fetch event:
adb shell cmd jobscheduler run -f com.my.app.id 999
TSBackgroundFetch: - Background Fetch event received
TSBackgroundFetch: - finish
TSBackgroundFetch: - jobFinished
TSBackgroundFetch: HeadlessJobService onStartJob

TSBackgroundFetch: onHeadlessJsTaskStart: 1  <--- TASK START

ReactNativeJS: [BackgroundFetch HeadlessTask] start
ReactNativeJS: '[BackgroundFetch] response: ', [ { device_id: 'ReactNative-Pixel-3a',
ReactNativeJS:     device_model: 'Pixel 3a (ReactNative)' },
ReactNativeJS:   { device_id: 'ReactNative-ANE-LX3',
ReactNativeJS:     device_model: 'ANE-LX3 (ReactNative)' },
ReactNativeJS:   { device_id: 'ReactNative-ONEPLUS-A5010',
ReactNativeJS:     device_model: 'ONEPLUS A5010 (ReactNative)' },
ReactNativeJS:   { device_id: 'ReactNative-SM-A520W',
ReactNativeJS:     device_model: 'SM-A520W (ReactNative)' },
ReactNativeJS:   { device_id: 'ReactNative-XT1068',
ReactNativeJS:     device_model: 'XT1068 (ReactNative)' },
ReactNativeJS:   { device_id: 'ReactNative-Pixel',
ReactNativeJS:     device_model: 'Pixel (ReactNative)' },
ReactNativeJS:   { device_id: 'ReactNative-TA-1027',
ReactNativeJS:     device_model: 'TA-1027 (ReactNative)' },
ReactNativeJS:   { device_id: 'ReactNative-Mi-A2-Lite',
ReactNativeJS:     device_model: 'Mi A2 Lite (ReactNative)' },
ReactNativeJS:   { device_id: 'ReactNative-Nexus-4',
ReactNativeJS:     device_model: 'Nexus 4 (ReactNative)' },
ReactNativeJS:   { device_id: 'ReactNative-SAMSUNG-SM-J320A',
ReactNativeJS:     device_model: 'SAMSUNG-SM-J320A (ReactNative)' },
ReactNativeJS:   { device_id: 'ReactNative-Nexus-5',
ReactNativeJS:     device_model: 'Nexus 5 (ReactNative)' },
.
.
.
ReactNativeJS:   } ]
ReactNativeJS: [BackgroundFetch HeadlessTask] finished
TSBackgroundFetch: - finish
TSBackgroundFetch: HeadlessJobService jobFinished
TSBackgroundFetch: onHeadlessJsTaskFinish: 1  <--- TASK FINISH
0reactions
Zilleabbas10commented, Jan 27, 2020

@christocracy Facing this issue in android 9 as well. Is there any way to track when OS will trigger the task. Because in android 6 it is working fine, task is trigger after 15 min either app is killed or in background state but this same thing is not working in Android 9 ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android background service with Headless JS is not working
Using this code, it runs but closes the app right away and it doesn't display any errors. Any idea why this is happening...
Read more >
Restrictions on starting activities from the background
Note: For the purposes of starting activities, an app running a foreground service is still considered to be "in the background". This guide...
Read more >
Headless JS - React Native
Headless JS is a way to run tasks in JavaScript while your app is in the background. It can be used, for example,...
Read more >
Android 13 changelog: A deep dive by Mishaal Rahman
This article documents all of the changes we found in Android 13 so developers can prepare their applications or devices while users can...
Read more >
react-native-background-task - Bountysource
Created 5 years ago in jamesisaac/react-native-background-task with 14 comments. I used the Simple example provided in your readme and I can't see the...
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