Closed ifgabriel closed 10 months ago
Can you tell me how did you fix it because I see you set it as completed? Thank you so much
@Tanani1994 I gave up using this library, and use react-native-background-actions
I used this code
import BackgroundService from 'react-native-background-actions';
useEffect(() => {
const handlePermission = async () => {
await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_BACKGROUND_LOCATION, {
title: '',
message: '',
buttonPositive: 'OK',
});
};
const handleLocation = async () => {
const handleTask = async () => {
if (BackgroundService.isRunning()) {
BackgroundService.updateNotification(notificationOptions);
await new Promise(() => {
Geolocation.watchPosition(
({ coords, mocked }) => {
(YOUR FUNCTION)
},
undefined,
{
enableHighAccuracy: true,
distanceFilter: 0,
interval: 30000,
forceRequestLocation: true,
showLocationDialog: true,
}
);
});
} else {
BackgroundService.stop();
}
};
if (!BackgroundService.isRunning() && !!dutyId) {
await BackgroundService.start(handleTask, notificationOptions);
}
};
handlePermission();
handleLocation();
}, [mutate, occurrenceId, dutyId, notificationOptions]);
These permissions are required
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
I hope it helped you
useEffect(() => { const handlePermission = async () => { await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_BACKGROUND_LOCATION, { title: '', message: '', buttonPositive: 'OK', }); };
const handleLocation = async () => { const handleTask = async () => { if (BackgroundService.isRunning()) { BackgroundService.updateNotification(notificationOptions); await new Promise(() => { Geolocation.watchPosition( ({ coords, mocked }) => { (YOUR FUNCTION) }, undefined, { enableHighAccuracy: true, distanceFilter: 0, interval: 30000, forceRequestLocation: true, showLocationDialog: true, } ); }); } else { BackgroundService.stop(); } }; if (!BackgroundService.isRunning() && !!dutyId) { await BackgroundService.start(handleTask, notificationOptions); } }; handlePermission(); handleLocation();
}, [mutate, occurrenceId, dutyId, notificationOptions]);
Can you explain what does this mutate, occurenceId, means and where is this defined is it in utils or screens?
Platforms
Android
Versions
Please add the used versions/branches
AndroidManifest
Code
Expected
I'm trying to send a user's location to my API every 10 seconds. This should work if the screen is off, if the phone is using another app, etc. But this is not working correctly. I already used the backround example in src/example and it didn't work. Can anyone help me?