Not able to run headless task in background.[Android-9]
See original GitHub issueYour 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:
- Created 4 years ago
- Comments:12 (4 by maintainers)
Top GitHub Comments
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
@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 ?