react-native-background-geolocation icon indicating copy to clipboard operation
react-native-background-geolocation copied to clipboard

Background update not happening

Open RaviKumar-msys opened this issue 2 years ago • 3 comments

Location update Foreground and Background

Your Environment

  • Plugin version:
  • Platform: iOS or Android
  • OS version:
  • Device manufacturer / model:
  • React Native version (react-native -v):
  • Plugin config
PASTE_YOUR_CODE_HERE
import React, { useEffect, useState } from 'react';
import Geolocation from 'react-native-geolocation-service';
import BackgroundGeolocation from 'react-native-background-geolocation';

const useLocation = () => {
  const [location, setLocation] = useState(null);

  useEffect(() => {
    let isMounted = true;

    // Request foreground location updates
    Geolocation.getCurrentPosition(
      position => {
        if (isMounted) {


          setLocation({
            latitude: position.coords.latitude,
            longitude: position.coords.longitude,
          });
        }
      },
      error => {
        console.log('Foreground location error: ', error);
      },
      { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
    );

    // Configure background location updates
    BackgroundGeolocation.configure({
      desiredAccuracy: BackgroundGeolocation.HIGH_ACCURACY,
      stationaryRadius: 50,
      distanceFilter: 1,
      notificationTitle: 'Background tracking',
      notificationText: 'enabled',
      debug: false,
      startOnBoot: false,
      stopOnTerminate: true,
      locationProvider: BackgroundGeolocation.ACTIVITY_PROVIDER,
      interval: 10000,
      fastestInterval: 5000,
      activitiesInterval: 10000,
      stopOnStillActivity: false,
    });

    // Register background location event handler
    BackgroundGeolocation.on('location', location => {
      if (isMounted) {
        setLocation({
          latitude: location.latitude,
          longitude: location.longitude,
        });
      }
    });

    // Start background location tracking
    BackgroundGeolocation.start();

    return () => {
      isMounted = false;

      // Stop background location tracking when component unmounts
      BackgroundGeolocation.stop();
      BackgroundGeolocation.removeAllListeners();
    };
  }, []);

  return location;
};

export default useLocation;

Expected Behavior

Actual Behavior

Steps to Reproduce

Context

I want to fetch location update in the foreground and background, I am using an above custom hook to get location, i don't know where went wrong, it's not working My requirement

  1. Continuous fetch location foreground and background

Debug logs

Logs
PASTE_YOUR_LOGS_HERE

RaviKumar-msys avatar Jul 05 '23 10:07 RaviKumar-msys

Your Environment

  • Plugin version:
  • Platform: iOS or Android
  • OS version:
  • Device manufacturer / model:
  • React Native version (react-native -v):
  • Plugin config
PASTE_YOUR_CODE_HERE

christocracy avatar Jul 05 '23 13:07 christocracy

  • see wiki “Debugging”
  • See wiki “Philosophy of Operation”

christocracy avatar Jul 05 '23 13:07 christocracy

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar May 19 '24 01:05 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Jun 02 '24 01:06 github-actions[bot]