Setting up TaskManager


I’m trying to set up background location tracking with the new SDK 32 release. I read that TaskManager.defineTask() needs to be called in the global scope of my app. I have it being called in App.js

import { TaskManager } from 'expo';
import { BACKGROUND_LOCATION_UPDATES_TASK} from './tasks';

TaskManager.defineTask(BACKGROUND_LOCATION_UPDATES_TASK, ({data, error}) => {
            const { locations } = data;
            console.log('locations: ', + locations);
        } catch(error){

I have the initiation of Location.startLocationUpdatesAsync() in my settings screen. When the user toggles the switch to on, the location update should start. Here is an example of the logic.

toggleLocationTracking = async() => {
     const { trackingOn } = this.state;
     const { status } = await Permissions.askAsync(Permissions.LOCATION);
     let isRegistered = await 
     if(!trackingOn && isRegistered && status=='granted'){
            await Location.startLocationUpdatesAsync(BACKGROUND_LOCATION_UPDATES_TASK, {
                accuracy: Location.Accuracy.High,
                timeInterval: 2500,
                distanceInterval: 5,
                showsBackgroundLocationIndicator: false
     ... more code

Additionally, when the user toggles the switch off, i run Location.stopLocationUpdatesAsync(BACKGROUND_LOCATION_UPDATES_TASK);

When I try to toggle on my switch, the task is still not registered.


After going back and reading through the documentation for Location.startLocationUpdatesAsync(),
I realized that running this method is what actually registers the task. I shouldn’t be checking for “isRegistered” until afterwards when turning location tracking off.

1 Like

Glad you got it figured out. When in doubt, check the docs haha.

closed #4

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