react-native-background-fetch从不执行回调函数

问题描述 投票:0回答:1

适用于安卓系统 我创建了 startStask 函数来使用“react-native-background-fetch”中的 BackgroundFetch 启动后台任务,如下所示:

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);
  }
};

但是回调永远不会被调用(“try ok”之后没有日志)并且locationAndNotificationTask也不会被调用。 这个函数是通过像这样的切换动作来调用的:

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
  • 最小SdkVersion = 21
  • 编译SdkVersion = 34
  • 目标SdkVersion = 34
  • react-native-background-fetch:“^4.2.1”,

谢谢。

android react-native background-task react-native-background-fetch
1个回答
0
投票

您找到解决方案了吗?我也遇到同样的问题

© www.soinside.com 2019 - 2024. All rights reserved.