capacitor-community / capacitor-community/background-geolocation
Is called foreground
- Dominant language
- Java
- Stars
- 249
- Forks
- 79
- PR merge metrics
- No merged PRs in 30d
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('BackgroundGeolocation');
export const useNativeGeolocationBackground = (): IGeolocation => {
const watchIdRef = useRef(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 => {
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**
https://github.com/user-attachments/assets/ee1a9e4d-9470-4ebe-8983-8081f621a2d1
**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]
Contributor guide
Research direction
No repository file or test is named. Start at the BackgroundGeolocation.addWatcher entry point used by startGeolocation and compare its callback behavior on the reported iOS and Android setups. Done means the watcher callback is invoked only while the app is in the background, with the foreground behavior verified on both reported platforms.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react, typescript
- Domain
- mobile
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100