transistorsoft / react-native-background-geolocation

Sophisticated, battery-conscious background-geolocation with motion-detection
http://shop.transistorsoft.com/pages/react-native-background-geolocation
MIT License
2.54k stars 424 forks source link

can't get location it's always timeout #1958

Closed Nader-CS closed 2 weeks ago

Nader-CS commented 2 months ago

Your Environment

Expected Behavior

to get current user location

Actual Behavior

always give me timeout with error 408

Steps to Reproduce

 * This hook will be used to access user location and send it to the server
 * features:
 * - get user location [done].
 * - send user location to server [done].
 */

import {useCallback, useEffect, useMemo} from 'react';
import BackgroundGeolocation from 'react-native-background-geolocation';
import {useState} from 'react';
import {useDispatch, useSelector} from 'react-redux';
import {getDriverLocation, getToken, getCurrentOrder} from '@selectors';
import Config from 'react-native-config';
import colors from '@common/colors';
import {t} from 'i18next';
import {setDriverLocation} from '@slices';
import {useGetUserQuery} from '@services/user';
import crashlytics from '@react-native-firebase/crashlytics';

const notificationConfig = {
  title: t('hive'),
  text: t('you_are_online'),
  color: colors.notification,
  largeIcon: 'drawable/hive_logo',
  smallIcon: 'drawable/hive_logo',
  sticky: true,
};

const useTracking = () => {
  const dispatch = useDispatch();
  const {data: driverData} = useGetUserQuery();
  const {access_token} = useSelector(getToken) || {};
  const [isReady, setIsReady] = useState(false);
  const location = useSelector(getDriverLocation);
  const order = useSelector(getCurrentOrder);
  const configuration = useMemo(() => {
    return {
      desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_MEDIUM,
      distanceFilter: 250,
      stopOnStationary: true,
      stopTimeout: 5,
      debug: __DEV__,
      logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
      stopOnTerminate: false,
      startOnBoot: true,
      batchSync: false,
      autoSync: true,
      foregroundService: true,
      enableHeadless: true,
      notification: notificationConfig,
      url: `${Config.API_URL}api/v1/driver_locations`,
      method: 'POST',
      headers: {
        Authorization: `Bearer ${access_token}`,
      },
      params: {orderId: order?.id},
    };
  }, [order?.id, access_token]);

  useEffect(() => {
    let locationSubscription;
    BackgroundGeolocation.ready({
      ...configuration,
    })
      .then(() => {
        locationSubscription = BackgroundGeolocation.onLocation(
          location => dispatch(setDriverLocation(location.coords)),
          error => {
            crashlytics().recordError(error);
          },
        );

      })
      .catch(error => {
        crashlytics().recordError(error);
      })
      .finally(() => {
        getLocation();
      });

    return () => {
      locationSubscription.remove();
    };
  }, []);

  const getLocation = async () => {
    const location = await BackgroundGeolocation.getCurrentPosition({
      samples: 1,
      persist: false,
    });

    dispatch(setDriverLocation(location.coords));

    return location;
  };

  const startTracking = () => {
    BackgroundGeolocation.start();
  };

  const stopTracking = () => {
    BackgroundGeolocation.stop();
  };

  const restartGeolocationService = useCallback(() => {
    BackgroundGeolocation.stop().then(() => {
      BackgroundGeolocation.setConfig({...configuration}).then(() => {
        BackgroundGeolocation.start();
      });
    });
  }, [configuration]);
  return {
    isReady,
    getLocation,
    startTracking,
    stopTracking,
    location,
    restartGeolocationService,
  };
};
export default useTracking;

Context

Debug logs

Logs ``` PASTE_YOUR_LOGS_HERE ```
Nader-CS commented 2 months ago

when i restart the phone , it working!

github-actions[bot] commented 4 weeks ago

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

github-actions[bot] commented 2 weeks ago

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