Don't get background location

Please provide the following:

  1. SDK Version: 48.0.6
  2. Platforms(Android/iOS/web/all): All
  3. Add the appropriate “Tag” based on what Expo library you have a question on.
    TaskManager
    startLocationUpdatesAsync

On expo Go. The script console.log the location every 1s when the app is foreground but stops when not foregrounded. Can you help me to find out where that comes from ?

The script is call in AppNavigator (when user is logged in) :

const AppNavigator = () => {
  useBackgroundLocation()
.....

The script in useBackground :

import { useEffect } from "react";
import * as Device from "expo-device";
import * as Location from "expo-location";
import * as TaskManager from "expo-task-manager";

import logger from "../utility/logger";
import backgroundLocationApi from "../api/backgroundLocationApi";

const LOCATION_TASK_NAME = "background-location-task";

TaskManager.defineTask(LOCATION_TASK_NAME, ({ data, error }) => {
  try {
    if (error) {
      logger.log(
        "🚀 ~ file: useBGLoc.js:28 ~ TaskManager.defineTask ~ error",
        error
      );
      return;
    }

    const { locations } = data;

    const position = {
      coordinates: {
        lat: locations[0].coords.latitude,
        lng: locations[0].coords.longitude,
      },
      speed: locations[0].coords.speed,
      altitude: locations[0].coords.altitude,
      timestamp: locations[0].timestamp,
    };

    console.log(
      "🚀 ~ position:",
      Device.osName + " " + Device.osVersion + JSON.stringify(position)
    );

    backgroundLocationApi.sendLocation(position);

    return Location.hasStartedLocationUpdatesAsync(LOCATION_TASK_NAME);
  } catch (error) {
    logger.log(
      "🚀 ~ file: useBackgroundLocation.js:57 ~ TaskManager.defineTask ~ error:",
      error
    );
  }
});

export default useBackgroundLocation = () => {

  useEffect(() => {
    (async () => {
      try {
        const { status: foregroundStatus } =
          await Location.requestForegroundPermissionsAsync();
        if (foregroundStatus === "granted") {
          const { status: backgroundStatus } =
            await Location.requestBackgroundPermissionsAsync();
          if (backgroundStatus !== "granted") {
            return;
          }
        } else {
          return;
        }

        const options = {
          accuracy: Location.Accuracy.High,
          distanceInterval:1000,
          timeInterval:1000,

          deferredUpdatesDistance: 0,
          distanceInterval: 0,
          showsBackgroundLocationIndicator: true, // Only available on iOS
        };

       const foregroundTask = await Location.startLocationUpdatesAsync(
          LOCATION_TASK_NAME,
          options
        );

        const isTaskRegistered = await TaskManager.isTaskRegisteredAsync(
          LOCATION_TASK_NAME
        );

        return () => {
          Location.stopLocationUpdatesAsync(LOCATION_TASK_NAME);

          if (isTaskRegistered) {
            TaskManager.unregisterTaskAsync(LOCATION_TASK_NAME);
          }
        };


      } catch (error) {
        logger.log(
          "🚀 ~ file: useBackgroundLocation.js:useEffect ~ error",
          error
        );
      }
    })();
  }, []);
};

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