Skip to content

[Help Wanted]: No Real-Time Location Tracking in Android Background Mode (Paid Version) #2351

@Jaimin2603

Description

@Jaimin2603

Required Reading

  • Confirmed

Plugin Version

4.18.7

Mobile operating-system(s)

  • iOS
  • Android

Device Manufacturer(s) and Model(s)

Galaxy M14 5G

Device operating-systems(s)

Android 14

React Native / Expo version

No response

What do you require assistance about?

I am reaching out regarding an issue with the paid version of react-native-background-geolocation in my React Native application. Specifically, real-time location tracking is not working when the app is in the background on Android, resulting in a straight line between the last foreground location and the new foreground location when the app resumes. The issue persists even when the device is locked, and I am using the paid version of the library to ensure robust background tracking.
This works for IOS and in the foreground on Android also.
Here is my code :

const setupBackgroundGeolocation = async () => {
    if (isInitializingRef.current) return;
    isInitializingRef.current = true;

    try {
      // 1. Register event listeners FIRST (before ready)
      BackgroundGeolocation.onLocation(
        location => {
          checkGPSWarmUp(location);
          updatePosition(location.coords, location.odometer);
          sendErrorReport(location, 'onLocation_trip_tracking');
        },
        error => {
          console.error('[onLocation] ERROR:', error);
          sendErrorReport(error, 'onLocation_error_trip_tracking');
        },
      );

      // 2. Configure and initialize
      await BackgroundGeolocation.ready({
        desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
        distanceFilter: 5, // Minimum distance between location updates (meters)
        stationaryRadius: 10, // Radius for stationary detection (meters)
        locationUpdateInterval: 1000,
        fastestLocationUpdateInterval: 500,
        activityRecognitionInterval: 5000,
        disableMotionActivityUpdates: false,
        stopOnStationary: false, // Don't stop when stationary
        preventSuspend: true,
        locationAuthorizationRequest: 'Always',
        allowsBackgroundLocationUpdates: true,
        pausesLocationUpdatesAutomatically: false,
        showsBackgroundLocationIndicator: true,
        debug: true, // Enable logging during development
        stopOnTerminate: false,
        startOnBoot: true,
        foregroundService: true,
        enableHeadless: true,
        disableElasticity: true,
        heartbeatInterval: 60, // Paid version: trigger every 60 seconds
        notification: {
          title: 'Trip Tracking',
          text: 'Tracking your location in the background',
          smallIcon: 'ic_stat_notify', // Ensure icon exists in res/drawable
        },
        logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
        activityType: BackgroundGeolocation.ACTIVITY_TYPE_OTHER_NAVIGATION,
      });

      isInitializingRef.current = false;
    } catch (error) {
      console.error('Error setting up background geolocation:', error);
      sendErrorReport(error, 'setup_bg_geolocation_error');
      isInitializingRef.current = false;
    }
  };

  const cleanupBackgroundGeolocation = async () => {
    try {
      console.log('BackgroundGeolocation stoppedn4444444');
      await BackgroundGeolocation.stop();
      console.log('[CamDash] BackgroundGeolocation stopped');
    } catch (error) {
      sendErrorReport(error, 'cleanup_bg_geolocation_error');
    }
  };

  // Initialize once when component mounts
  useEffect(() => {
    // Setup the plugin first, don't call getCurrentLocation before ready
    setupBackgroundGeolocation();

    return () => {
      // Only stop, don't remove listeners
      cleanupBackgroundGeolocation();
    };
  }, []);

[Optional] Plugin Code and/or Config

[Optional] Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions