Open
Description
Describe the bug
When the app starts I call the watch but it is called in the foreground and not in the background
To Reproduce
import { useRef } from 'react';
import { BackgroundGeolocationPlugin, Location } from '@capacitor-community/background-geolocation';
import { registerPlugin } from '@capacitor/core';
import { GeolocationData, IGeolocation } from '../geolocation.type';
const BackgroundGeolocation = registerPlugin<BackgroundGeolocationPlugin>('BackgroundGeolocation');
export const useNativeGeolocationBackground = (): IGeolocation => {
const watchIdRef = useRef<string | null>(null);
const startGeolocation = async () => {
watchIdRef.current = await BackgroundGeolocation.addWatcher(
{
backgroundMessage: 'App is tracking your location in background',
backgroundTitle: 'Background Tracking',
requestPermissions: true,
stale: false,
distanceFilter: 50,
},
(position, error) => {
if (error) {
if (error.code === 'NOT_AUTHORIZED') {
if (window.confirm('Location access is denied. Open settings to grant access?')) {
BackgroundGeolocation.openSettings();
}
}
console.error('Error during background tracking', error);
}
if (position) {
const { latitude, longitude, accuracy, time } = position;
console.log('Background position:', latitude, longitude);
return {
latitude,
longitude,
accuracy,
timestamp: time,
};
} else {
throw new Error('Position is undefined');
}
},
);
};
const startTracking = async () => {
try {
console.info('Starting background geolocation');
startGeolocation();
} catch (error) {
console.error('Error starting background geolocation', error);
}
};
const stopTracking = async () => {
try {
if (watchIdRef.current !== null) {
console.info('Stopping background geolocation');
await BackgroundGeolocation.removeWatcher({ id: watchIdRef.current });
watchIdRef.current = null;
}
} catch (error) {
console.error('Error stopping background geolocation', error);
}
};
const getCurrentPosition = async (): Promise<GeolocationData> => {
let lastLocation: Location | undefined;
try {
const id = await BackgroundGeolocation.addWatcher(
{
requestPermissions: false,
stale: true,
},
location => {
lastLocation = location || undefined;
},
);
if (lastLocation) {
await new Promise(resolve => setTimeout(resolve, 10000));
await BackgroundGeolocation.removeWatcher({ id });
return {
coords: {
...lastLocation,
},
timestamp: Date.now(),
};
}
throw new Error(`Error current position is: ${lastLocation}`);
} catch (error) {
throw new Error(`Error while getting current position: ${error}`);
}
};
return { startTracking, stopTracking, getCurrentPosition };
};
Expected behavior
the expected behavior should be that it is only called in the background
Screenshots
issue-background.1.mp4
Desktop (please complete the following information):
- OS: IOS
- Browser: chrome
- Version [e.g. 22]
Smartphone (please complete the following information):
- Device: Moto (motorola) g73 5g (XT2237-2)
- OS: Android 14
- Browser chrome
- Version [e.g. 22]
Metadata
Metadata
Assignees
Labels
No labels