Skip to content

Latest commit

 

History

History
113 lines (86 loc) · 3.55 KB

task-manager.mdx

File metadata and controls

113 lines (86 loc) · 3.55 KB
title description sourceCodeUrl packageName platforms
TaskManager
A library that provides support for tasks that can run in the background.
expo-task-manager
android
ios

import { APIInstallSection } from '/components/plugins/InstallSection'; import APISection from '/components/plugins/APISection'; import { SnackInline } from '/ui/components/Snippet'; import { ConfigReactNative } from '/components/plugins/ConfigSection';

expo-task-manager provides an API that allows you to manage long-running tasks, in particular those tasks that can run while your app is in the background. Some features of this module are used by other modules under the hood. Here is a list of Expo modules that use TaskManager:

Installation

Configuration in app.json/app.config.js

Background modes on iOS

TaskManager works out of the box in the Expo Go app on Android. However, on iOS, you'll need to use a development build.

Standalone apps need some extra configuration: on iOS, each background feature requires a special key in UIBackgroundModes array in your Info.plist file. In standalone apps this array is empty by default, so to use background features you will need to add appropriate keys to your app.json configuration.

Here is an example of an app.json configuration that enables background location, background fetch and remote notifications:

{
  "expo": {
    "ios": {
      "infoPlist": {
        "UIBackgroundModes": ["location", "fetch", "remote-notification"]
      }
    }
  }
}

Learn how to configure the native projects in the installation instructions in the expo-task-manager repository.

Example

<SnackInline dependencies={["expo-task-manager", "expo-location"]} platforms={['android', 'ios']}>

import React from 'react';
import { Button, View, StyleSheet } from 'react-native';
import * as TaskManager from 'expo-task-manager';
import * as Location from 'expo-location';

const LOCATION_TASK_NAME = 'background-location-task';

const requestPermissions = async () => {
  const { status: foregroundStatus } = await Location.requestForegroundPermissionsAsync();
  if (foregroundStatus === 'granted') {
    const { status: backgroundStatus } = await Location.requestBackgroundPermissionsAsync();
    if (backgroundStatus === 'granted') {
      await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
        accuracy: Location.Accuracy.Balanced,
      });
    }
  }
};

const PermissionsButton = () => (
  <View style={styles.container}>
    <Button onPress={requestPermissions} title="Enable background location" />
  </View>
);

TaskManager.defineTask(LOCATION_TASK_NAME, ({ data, error }) => {
  if (error) {
    // Error occurred - check `error.message` for more details.
    return;
  }
  if (data) {
    const { locations } = data;
    // do something with the locations captured in the background
  }
});

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
});

export default PermissionsButton;

API

import * as TaskManager from 'expo-task-manager';