react-native-background-fetch: Cannot Schedule Background Task in Background in IOS

Your Environment

  • Plugin version: 3.0.4
  • Platform: iOS or Android – iOS
  • OS version: 13.3
  • Device manufacturer / model: IOS
  • React Native version (react-native -v): 0.62.2
  • Plugin config
  BackgroundFetch.configure({
    minimumFetchInterval: 15,
    stopOnTerminate:false,
    forceAlarmManager: true,
    enableHeadless:true
  }, async (taskId) => {
    console.log("[BackgroundFetch] taskId: ", taskId);
    BackgroundFetch.finish(taskId);
  });

Expected Behavior

IT should output console.log("[BackgroundFetch] taskId: ", taskId); even when the App is Killed or just in Background on Ios

Actual Behavior

I get this Error [TSBackgroundFetch scheduleTask] ERROR: Failed to submit task request: Error Domain=TSBackgroundFetch Code=0 "Background procssing task was not registered in AppDelegate didFinishLaunchingWithOptions. See iOS Setup Guide."

Steps to Reproduce

in App.js

componentDidMount(){
  
  BackgroundFetch.configure({
    minimumFetchInterval: 15,
    stopOnTerminate:false,
    forceAlarmManager: true,
    enableHeadless:true
  }, async (taskId) => {
    console.log("[BackgroundFetch] taskId: ", taskId);
    BackgroundFetch.finish(taskId);
  });
}

On Click Handle that schedules it

testbackground = () =>{
// And with with #scheduleTask
BackgroundFetch.scheduleTask({
  stopOnTerminate: false,
  enableHeadless: true,
  taskId: 'com.foo.customtask',
  delay: 5000,       // milliseconds
  forceAlarmManager: true,
  periodic: false
});
}

In index.js

import BackgroundFetch from "react-native-background-fetch";

let MyHeadlessTask = async (event) => {
  // Get task id from event {}:
  let taskId = event.taskId;
  console.log('[BackgroundFetch HeadlessTask] start: ', taskId);

  // Perform an example HTTP request.
  // Important:  await asychronous tasks when using HeadlessJS.
  let response = await fetch('https://facebook.github.io/react-native/movies.json');
  let responseJson = await response.json();
  console.log('[BackgroundFetch HeadlessTask] response: ', responseJson);

  // Required:  Signal to native code that your task is complete.
  // If you don't do this, your app could be terminated and/or assigned
  // battery-blame for consuming too much time in background.
  BackgroundFetch.finish(taskId);
}

// Register your BackgroundFetch HeadlessTask BackgroundFetch.registerHeadlessTask(MyHeadlessTask);

Context

I am trying to run a task in Background, just to see how it works

Debug logs

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 17 (7 by maintainers)

Most upvoted comments

hmm, Could i try with just com.transistor.fetch ?

No. That’s for the traditional periodic fetch-task.

Have you entered the text com.foo.customtask in your Info.plist within Permitted background task scheduler identifiers, as documented in the Setup Instructions?

If you intend to execute your own custom tasks via BackgroundFetch.scheduleTask, you must add those custom identifiers as well. For example, if you intend to execute a custom taskId: ‘com.transistorsoft.customtask’, you must add the identifier com.transistorsoft.customtask to your “Permitted background task scheduler identifiers”, as well.

You’ve registered your com.foo.customtask in Info.plist?

Thank You @christocracy . That initial Error is gone and i think it works… But i get this message from xcode console whenever the app has gone to the background 2020-05-03 18:51:55.208245+0100 xxx[1947:685790] Can't end BackgroundTask: no background task exists with identifier 12 (0xc), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.