Expo

Get Started
API Reference

BackgroundFetch

expo-background-fetch provides an API to perform background fetch tasks, allowing you to run specific code periodically in the background to update your app. This module uses TaskManager Native API under the hood.

Platform Compatibility

Android DeviceAndroid EmulatoriOS DeviceiOS SimulatorWeb

Known issues

iOS only: BackgroundFetch only works when the app is backgrounded, not if the app was terminated or upon device reboot. Here is the relevant Github issue

For managed apps, you'll need to run expo install expo-background-fetch. To use it in bare React Native app, follow its installation instructions;

Below is an example that demonstrates how to use expo-background-fetch.
import React from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
import * as BackgroundFetch from 'expo-background-fetch';
import * as TaskManager from 'expo-task-manager';

const BACKGROUND_FETCH_TASK = 'background-fetch';

// 1. Define the task by providing a name and the function that should be executed
// Note: This needs to be called in the global scope (e.g outside of your React components)
TaskManager.defineTask(BACKGROUND_FETCH_TASK, async () => {
  const now = Date.now();

  console.log(`Got background fetch call at date: ${new Date(now).toISOString()}`);

  // Be sure to return the successful result type!
  return BackgroundFetch.Result.NewData;
});

// 2. Register the task at some point in your app by providing the same name, and some configuration options for how the background fetch should behave
// Note: This does NOT need to be in the global scope and CAN be used in your React components!
async function registerBackgroundFetchAsync() {
  return BackgroundFetch.registerTaskAsync(BACKGROUND_FETCH_TASK, {
    minimumInterval: 60 * 15, // 15 minutes
    stopOnTerminate: false, // android only,
    startOnBoot: true, // android only
  });
}

// 3. (Optional) Unregister tasks by specifying the task name
// This will cancel any future background fetch calls that match the given name
// Note: This does NOT need to be in the global scope and CAN be used in your React components!
async function unregisterBackgroundFetchAsync() {
  return BackgroundFetch.unregisterTaskAsync(BACKGROUND_FETCH_TASK);
}

export default function BackgroundFetchScreen() {
  const [isRegistered, setIsRegistered] = React.useState<boolean>(false);
  const [status, setStatus] = React.useState<BackgroundFetch.Status | null>(null);

  React.useEffect(() => {
    checkStatusAsync();
  }, []);

  const checkStatusAsync = async () => {
    const status = await BackgroundFetch.getStatusAsync();
    const isRegistered = await TaskManager.isTaskRegisteredAsync(BACKGROUND_FETCH_TASK);
    setStatus(status);
    setIsRegistered(isRegistered);
  };

  const toggleFetchTask = async () => {
    if (isRegistered) {
      await unregisterBackgroundFetchAsync();
    } else {
      await registerBackgroundFetchAsync();
    }

    checkStatusAsync();
  };

  return (
    <View style={styles.screen}>
      <View style={styles.textContainer}>
        <Text>
          Background fetch status:{' '}
          <Text style={styles.boldText}>{status ? BackgroundFetch.Status[status] : null}</Text>
        </Text>
        <Text>
          Background fetch task name:{' '}
          <Text style={styles.boldText}>
            {isRegistered ? BACKGROUND_FETCH_TASK : 'Not registered yet!'}
          </Text>
        </Text>
      </View>
      <View style={styles.textContainer}></View>
      <Button
        title={isRegistered ? 'Unregister BackgroundFetch task' : 'Register BackgroundFetch task'}
        onPress={toggleFetchTask}
      />
    </View>
  );
}

%%placeholder-start%%%%placeholder-end%%const styles = StyleSheet.create({
  screen: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },

  textContainer: {
    margin: 10,
  },
  boldText: {
    fontWeight: 'bold',
  },
});

Background fetches can be difficult to test because they can happen inconsistently. Fortunately, you can trigger background fetches manually when developing your apps.
For iOS, you can use the Instruments app on macOS to manually trigger background fetches:
  1. Open the Instruments app. The Instruments app can be searched through Spotlight (⌘ + Space) or opened from /Applications/Xcode.app/Contents/Applications/Instruments.app
  2. Select Time Profiler
  3. Select your device / simulator and pick the Expo Go app
  4. Press the Record button in the top left corner
  5. Navigate to the Document Menu and select Simulate Background Fetch - Expo Go:
Xcode Menu with Simulate Background Fetch option
For Android, you can set the minimumInterval option of your task to a small number and background your application like so:
async function registerBackgroundFetchAsync() {
  return BackgroundFetch.registerTaskAsync(BACKGROUND_FETCH_TASK, {
    minimumInterval: 1, // task will fire 1 minute after app is backgrounded
  });
}

In order to use BackgroundFetch API in standalone, detached and bare apps on iOS, your app has to include background mode in the Info.plist file. See background tasks configuration guide for more details.
On Android, this module might listen when the device is starting up. It's necessary to continue working on tasks started with startOnBoot. It also keeps devices "awake" that are going idle and asleep fast, to improve reliability of the tasks. Because of this both the RECEIVE_BOOT_COMPLETED and WAKE_LOCK permissions are added automatically.

import * as BackgroundFetch from 'expo-background-fetch';

Gets a status of background fetch.

  • Promise<BackgroundFetchStatus | null>

Returns a promise which fulfils with one of BackgroundFetchStatus enum values.


  • taskName (string) - Name of the task to register. The task needs to be defined first - see TaskManager.defineTask for more details.
  • options (BackgroundFetchOptions) - An object containing the background fetch options.

    Example

    import * as BackgroundFetch from 'expo-background-fetch';
    import * as TaskManager from 'expo-task-manager';
    
    TaskManager.defineTask(YOUR_TASK_NAME, () => {
      try {
        const receivedNewData = // do your background fetch here
        return receivedNewData ? BackgroundFetch.Result.NewData : BackgroundFetch.Result.NoData;
      } catch (error) {
        return BackgroundFetch.Result.Failed;
      }
    });

Registers background fetch task with given name. Registered tasks are saved in persistent storage and restored once the app is initialized.


  • minimumInterval (number) - Number of seconds that must elapse before another background fetch can be called.

Sets the minimum number of seconds that must elapse before another background fetch can be initiated. This value is advisory only and does not indicate the exact amount of time expected between fetch operations.

This method doesn't take any effect on Android. It is a global value which means that it can overwrite settings from another application opened through Expo Go.

A promise which fulfils once the minimum interval is set.


  • taskName (string) - Name of the task to unregister.

Unregisters background fetch task, so the application will no longer be executing this task.

A promise which fulfils when the task is fully unregistered.

NameTypeDescription
minimumInterval
(optional)
numberInexact interval in seconds between subsequent repeats of the background fetch alarm. The final interval may differ from the specified one to minimize wakeups and battery usage.
  • On Android it defaults to 10 minutes,
  • On iOS it calls BackgroundFetch.setMinimumIntervalAsync behind the scenes and the default value is the smallest fetch interval supported by the system (10-15 minutes). Background fetch task receives no data, but your task should return a value that best describes the results of your background fetch work.
startOnBoot
(optional)
boolean(Android only). Whether to restart background fetch events when the device has finished booting. Defaults to false.
stopOnTerminate
(optional)
boolean(Android only). Whether to stop receiving background fetch events after user terminates the app. Defaults to true.

This return value is to let iOS know what the result of your background fetch was, so the platform can better schedule future background fetches. Also, your app has up to 30 seconds to perform the task, otherwise your app will be terminated and future background fetches may be delayed.

  • BackgroundFetchResult.NoData : 1 - There was no new data to download.
  • BackgroundFetchResult.NewData : 2 - New data was successfully downloaded.
  • BackgroundFetchResult.Failed : 3 - An attempt to download data was made but that attempt failed.

  • BackgroundFetchStatus.Denied : 1 - The user explicitly disabled background behavior for this app or for the whole system.
  • BackgroundFetchStatus.Restricted : 2 - Background updates are unavailable and the user cannot enable them again. This status can occur when, for example, parental controls are in effect for the current user.
  • BackgroundFetchStatus.Available : 3 - Background updates are available for the app.