How to do background timer with Background-fetch from expo? Help!

This is my code:

Outside any react component:

import * as TaskManager from 'expo-task-manager';
import * as BackgroundFetch from 'expo-background-fetch';

const OFFLINE_TASK_NAME = 'background-offline-upload-task';

async function registerTask() {
  TaskManager.defineTask(OFFLINE_TASK_NAME, async () => {
    alert(`Running` + new Date().getTime());
    console.log('called once')
    return BackgroundFetch.Result.NewData;
  });
  await BackgroundFetch.registerTaskAsync(OFFLINE_TASK_NAME, {
    stopOnTerminate: true,
    startOnBoot: false,
    minimumInterval: 1
  });
  await BackgroundFetch.setMinimumIntervalAsync(1);  
  console.log(await TaskManager.getRegisteredTasksAsync());
}

registerTask();

The issue is: On both android and iOS I don’t get any kind of alert or console log called. What am I doing wrong here? Please help

Did you ever figure this out?

No. In the end I had to start the project from scratch again, but without expo. Now I’m able to have background timers much easily…

Thanks. I will let you know if I figure it out. I know it would be easier without expo, but it’s just the last piece I need. Gonna try to do lots of guess and check haha

Woah.

One issue, which I discovered after going through a ton of githubs and everything is that you need to use a physical device. Apparently it doesn’t work in the emulator. Honestly documentation is wack for background fetch. It’s newer so maybe that’s why. Never would have figured that out. I am getting a console.log for your last console.log though now at least. Think the other one not yet just because haven’t tried triggering it. Bedtime for me but I think that’s really the issue, will look at again tomorrow

What I learned yesterday: Use console.warn.

Console.log will show up on your server console when you are online & connected to the metro server

Console.warn will show up on the device even when there is no connection to the ‘metro server’ :slight_smile:

1 Like

Gotcha makes sense. But unfortunately the minimumInterval doesn’t seem to do anything, doesn’t repeat. Get the warning once and never again. Would think you should get it every second.

Getting ready to eject :frowning:

I implemented a timer in my project that essentially “runs” in the background. Depending on what you need it to do this could work if you just need to create a generic timer. I have an interval running I’m the foreground using state that keeps track of the time. Using AppState I captured the time when the app is backgrounded. When the app is foregrounded I capture the date again and compare it to the old date and subtract the difference thus giving it the impression it is running in the background. As for notifications I schedule a notification for the timer start time when the timer is started. If the timer is paused I cancel the notification and when it is resumed I reschedule the notification for the new time. This provides the functionality of a timer without needed to use any background logic. Hope this helps anyone with the same issue!

4 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.