Background Fetch not working after app is terminated.
See original GitHub issueApp.js
export default class App extends Component {
componentDidMount() {
BackgroundFetch.configure({
minimumFetchInterval: 15, // <-- minutes (15 is minimum allowed)
stopOnTerminate: false,
enableHeadless: true,
startOnBoot: true,
requiredNetworkType: BackgroundFetch.NETWORK_TYPE_NONE, // Default
requiresCharging: false, // Default
requiresDeviceIdle: false, // Default
requiresBatteryNotLow: false, // Default
requiresStorageNotLow: false // Default
}, () => {
console.log("[js] Received background-fetch event");
BackgroundFetch.finish(BackgroundFetch.FETCH_RESULT_NEW_DATA);
}, (error) => {
console.log("[js] RNBackgroundFetch failed to start");
});
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;
}
});
}
render() {
return null
}
};
index.js
import { AppRegistry } from 'react-native';
import App from './App';
import { name as appName } from './app.json';
import BackgroundFetch from "react-native-background-fetch";
let MyHeadlessTask = async () => {
console.log('[BackgroundFetch HeadlessTask] start');
let response = await fetch('https://facebook.github.io/react-native/movies.json%27');
let responseJson = await response.json();
console.log('[BackgroundFetch HeadlessTask response: ', responseJson);
BackgroundFetch.finish();
}
// Register your BackgroundFetch HeadlessTask
BackgroundFetch.registerHeadlessTask(MyHeadlessTask);
AppRegistry.registerComponent(appName, () => App);
Your Environment
- Plugin version: 2.0.1
- Platform: Android
- OS version: Pie
- Device manufacturer / model: Huawei P20 Pro
- React Native version: 0.59.8
- Plugin config
Expected Behavior
Background function should work when app is terminated.
Actual Behavior
In the screenshot provided we see that the app logs as expected in the foreground/minimized, but when terminated the terminal logs ReactInstanceManager.detachViewFromInstance(), and when trying to run “adb shell cmd jobscheduler run -f <your.application.id> 999” the terminal doesnt log anything.
Steps to Reproduce
- Start a new project and copy paste my 2 files.
- Install background fetch dependency.
- Run log and “adb shell cmd jobscheduler run -f <your.application.id> 999”.
Context
I copied the documentation examples, but its only working in the foreground/minimized. I think it has to do with the registerheadlesstask, but I dont know if the code I have given is enough to register it or not, and the documentation isn’t very clear.
Debug logs
- Android: $ adb logcat
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:12 (6 by maintainers)
Top Results From Across the Web
Background fetch after app is force-quit - Apple Developer
Currently my app works performs background fetches correctly when it has been backgrounded, but after force-quitting the app it stops fetching.
Read more >How to run code when your app is terminated
The problem is that many apps are likely to have background fetch enabled, all with different timers being triggered by the system.
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 >Background fetch is not working after killing the app-swift
The Background Fetching will NOT happen in your app after the user has killed it in the multitasking UI. This is by design....
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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@thuongtv-vn it’s worse than you think! Or better, if you are an apple user that wants long battery life.
Location services “use in background” is only granted by Apple if your app needs it as a core feature and in my experience they are strict on that.
Silent Push (that is: a cloud message sent to device that will not generate a notification for the user) have low delivery rates for apps and must not be relied on. You should assume they will not be delivered. A cloud message sent to device that will generate a notification will typically not wake the app, or if you have a notification extension it will run that native code for a brief moment (30s max) then app goes back to sleep
In general you just don’t get to run your app on iOS unless your user is interacting with it or it is generating visible notifications which the user has agreed to and keeps enabled.
No. iOS halt background-fetch / background-task when user terminates the app.
there is no such thing as stopOnTerminate: false for iOS