For android I created startStask function to start my background task with BackgroundFetch from 'react-native-background-fetch' like this :
const startStask = async () => {
try {
console.log('try ok');
await BackgroundFetch.configure(
{
minimumFetchInterval: 15,
stopOnTerminate: true,
startOnBoot: true,
},
async taskId => {
console.log('[BackgroundFetch] taskId: ', taskId);
await locationAndNotificationTask();
BackgroundFetch.finish(taskId);
},
error => {
console.log('[BackgroundFetch] failed to start');
},
);
} catch (error) {
console.log('start BG stask err', error);
}
};
But the call back is never called (no logs after "try ok") and locationAndNotificationTask is not called. This fonction is call with a toggle action like that :
const toggleSwitch = async () => {
const newValue = !isEnabled;
try {
console.log('okay ! ', newValue)
if (newValue) {
setIsEnabled(newValue); // true
await AsyncStorage.setItem('notifications_status', `${newValue}`);
backgroundTaskIsRunning() ? null : await startStask();
} else {
setIsEnabled(newValue); // false
await AsyncStorage.setItem('notifications_status', `${newValue}`);
await stopStask();
}
} catch (error) {
console.log('menuScreen err', error);
}
};
- React-native 0.73.2
- minSdkVersion = 21
- compileSdkVersion = 34
- targetSdkVersion = 34
- react-native-background-fetch: "^4.2.1",
thanks.