1

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.