transistorsoft/react-native-background-fetch

[QUESTION] I cant seem to figure out how to set up the background tasks

Closed this issue · 5 comments

I am sorry if I didn't follow the 'issue format' I just couln't fit it in.
But the registered tasks don't execute.

This is the code given in the examples and I tried to paste it in my code and nothing really happened.
I am using Expo for this and I have followed all the steps to set up on Expo.

The only problem is that I can't run the function in the background as it was supposed to

let MyHeadlessTask = async (event) => {
    // Get task id from event {}:
    console.log("registering")
    let taskId = event.taskId;
    let isTimeout = event.timeout;  // <-- true when your background-time has expired.
    if (isTimeout) {
      // This task has exceeded its allowed running-time.
      // You must stop what you're doing immediately finish(taskId)
      console.log('[BackgroundFetch] Headless TIMEOUT:', taskId);
      BackgroundFetch.finish(taskId);
      return;
    }
    console.log('[BackgroundFetch HeadlessTask] start: ', taskId);
  
    // Perform an example HTTP request.
    // Important:  await asychronous tasks when using HeadlessJS.
    let response = await fetch('https://reactnative.dev/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);

Sorry I am a bit new to all this!

I’m not sure you understand what the Android HeadlessTask is. That only runs in the specific case where the Android app is terminated; not merely when your app is in the background.

Go back and read the readme.

Also consult the readme to learn how to simulate tasks.

That code you posted (then deleted) does not look like anything from this plug-in.

This issue is stale because it has been open for 30 days with no activity.

This issue was closed because it has been inactive for 14 days since being marked as stale.