Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
aymanMFD opened this issue May 24, 2024 · 5 comments
Closed
Labels

Comments

@aymanMFD
Copy link

aymanMFD commented May 24, 2024

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!

@christocracy
Copy link
Member

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.

@christocracy
Copy link
Member

Also consult the readme to learn how to simulate tasks.

@christocracy
Copy link
Member

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

Copy link

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

@github-actions github-actions bot added the stale label Jun 27, 2024
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants