Skip to content

BackgroundGeolocation is not working in actual android and iOS device. While it works in foreground mode #594

Description

@Prabhat2510

I am using BackgroundGeolocation , and sending locations to server through my websocket. Everything is working fine when i application is running in foreground mode, but when i killed the app it is not working both in ios and android.
This is only happening in actual iphone in android devices. Everything is working fine on android and ios simulators.

I am using this code ->

useEffect(() => {
requestLocationPermissions();
if (!clientId) return;

// Make connection with WebSocket
let connection: { stop: () => Promise<any> };
const setupSignalR = async () => {
  connection = await startConnection();
};
setupSignalR();

const onProviderChange = (provider) => {
  console.log("[providerchange] -", provider);
  const isOnline = provider.status === 3 || provider.status === 4;
  setLocalOfflineState(!isOnline);
};

const geoConfig = {
  desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
  distanceFilter: 1,
  debug: false,
  logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
  stopOnTerminate: false,
  startOnBoot: true,
  batchSync: false,
  autoSync: false,
  preventSuspend: true,
  useSignificantChangesOnly: false,
  enableHeadless: true,
};

// Register provider change listener
BackgroundGeolocation.onProviderChange(onProviderChange);

const onLocation = async (location) => {
  console.log("location", location);

  if (location.sample) {
    console.log("Ignored sample location");
    return;
  }
  if (lastLocationUUID.current === location.uuid) {
    console.log("Duplicate location ignored");
    return;
  }
  lastLocationUUID.current = location.uuid;
  //   Sending unique location
  sendLocationToServer(location);
};

const onError = (error) => {
  console.warn("[location] ERROR -", error);
};

BackgroundGeolocation.onLocation(onLocation, onError);

// Now, configure BackgroundGeolocation
BackgroundGeolocation.ready(geoConfig, (state) => {
  if (!state.enabled) {
    BackgroundGeolocation.start(() => {
      console.log("- Start success");
    });
  }
});

// Cleanup listeners on unmount
return () => {
  BackgroundGeolocation.removeListeners();
};

}, [clientId]);

const requestLocationPermissions = async () => {
// Request Fine Location first
const fineLocation = await request(
PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION
);

if (fineLocation === RESULTS.GRANTED) {
  // Then request Background Location
  const backgroundLocation = await request(
    PERMISSIONS.ANDROID.ACCESS_BACKGROUND_LOCATION
  );
}

};

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions