0

正如我从文档中了解到的那样,您启动了一个默认任务(当您的应用程序最小化时运行)以及可选的无头任务(当您的应用程序终止时运行)。像这样:

//configure normal background task, set time interval as 15 minutes.
BackgroundFetch.configure({
  minimumFetchInterval: 15
}, async (taskId) => {
  console.log("[BackgroundFetch] taskId: ", taskId);

  // Finish, providing received taskId.
  BackgroundFetch.finish(taskId);
}, async (taskId) => {
  BackgroundFetch.finish(taskId);
});

let MyHeadlessTask = async (event) => {
  let taskId = event.taskId;
  let isTimeout = event.timeout;
  if (isTimeout) {
    BackgroundFetch.finish(taskId);
    return;
  }
  console.log('[BackgroundFetch HeadlessTask] start: ', taskId);

  // some task..

  BackgroundFetch.finish(taskId);
}

// Register your BackgroundFetch HeadlessTask, which also runs every 15 minutes, because of the above configuration of minimumFetchInterval.
BackgroundFetch.registerHeadlessTask(MyHeadlessTask);

我想要的是能够使用 minimumFetchInterval 配置仅注册一个无头任务。像这样的东西:

BackgroundFetch.registerHeadlessTask(MyHeadlessTask, {
 minimumFetchInterval: 15
});

但我知道registerHeadlessTask不接受配置输入。还有其他方法吗?

4

1 回答 1

1
    let MyHeadlessTask = async (event) => {
      let taskId = event.taskId;
      let isTimeout = event.timeout;
      if (isTimeout) {
        BackgroundFetch.finish(taskId);
        return;
      }
      console.log('[BackgroundFetch HeadlessTask] start: ', taskId);
    ///// try this ////////
      BackgroundFetch.scheduleTask({
      taskId: yournewtaskid,
      forceAlarmManager: true,
      delay: 5000,
      periodic: true
    });

  BackgroundFetch.finish(headlesstaskId);
}

现在在您的新任务中尝试直接工作,或者尝试在其中再次创建无头任务来完成您的工作

于 2021-06-08T14:07:41.727 回答