Reputation: 63
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);
}
};
thanks.
Upvotes: 1
Views: 348