Cannot Schedule Background Task in Background in IOS

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
christocracycommented, May 3, 2020

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.

1reaction
allindevelopercommented, May 3, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

iOS Background Execution Limits | Apple Developer Forums
The short answer to all of these is You can't. iOS puts strict limits on background execution. Its default behaviour is to suspend...
Read more >
BGTaskScheduler: Is it possible to schedule a background ...
Yes, you can schedule the next task when processing the current task. The code example in Using Background Tasks to Update Your App...
Read more >
Common Reasons for Background Tasks to Fail in iOS
Factors That Affect Background Task Execution. · Critically Low Battery · Low Power Mode · App Usage · App Switcher · Background App...
Read more >
{Concise} All about handling Background tasks in iOS 14,13 ...
This usually involves some easy-to-overlook error in bookkeeping that results in the app beginning a background task and not ending it. For example,...
Read more >
Background Modes Tutorial: Getting Started
Apple started allowing apps to do work in the background in 2010 with the release of iOS 4 and has evolved and improved...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found