I have configured the package to send HTTP requests to the given API every 60 seconds (fastest interval for testing purposes), but it is never sending anything. I have also tried it with locationTemplate (see commented code), but the same issue remains.
As a final resort, I decided to go with heartbeatInterval, but even that is not working as it should. It does not trigger every 60 seconds as is specified, but at irregular intervals (over 5 minutes).
useEffect(() => {
let onHttp = null;
const initBackgroundGeolocationService = async () => {
const userToken = await getStorageValue('user', true);
if (userToken?.token && userToken?.id) {
console.log('[Auth Debug] User ID:', userToken.id);
console.log('[Auth Debug] Token:', userToken.token?.substring(0, 20) + '...');
// Ready the plugin
BackgroundGeolocation.ready({
// Geolocation Config
desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_MEDIUM,
distanceFilter: 0,
// locationUpdateInterval: 60000,
// fastestLocationUpdateInterval: 60000,
disableMotionActivityUpdates: true,
// Activity Recognition
stopTimeout: 1,
isMoving: true, // Force moving state to keep polling
preventSuspend: true,
// Application config
debug: true,
heartbeatInterval: 60,
locationAuthorizationRequest: 'Always',
allowIdenticalLocations: true,
logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
stopOnTerminate: false,
enableHeadless: true,
foregroundService: true,
startOnBoot: true,
// Notification config
notification: {
title: "Background Tracking",
text: "Location tracking active",
channelName: "Location Tracking",
priority: BackgroundGeolocation.NOTIFICATION_PRIORITY_LOW,
sound: null,
},
// HTTP Config
// url: REPORT_USER_GPS_COORDINATES,
// method: 'POST',
batchSync: false,
autoSync: true,
autoSyncThreshold: 0,
maxRecordsToPersist: 0,
maxDaysToPersist: 0,
// headers: {
// 'Content-Type': 'application/x-www-form-urlencoded',
// 'Accept': 'application/json', // Ensure the server sends JSON response
// },
backgroundPermissionRationale: {
title: "Allow ESi Centro to access this device's location even when closed or not in use.",
message: "ESi Centro collects location data to enable full functionality of the geofence feature.",
positiveAction: "Change to 'Always Allow'",
negativeAction: "Cancel",
},
// params: {
// user_id: String(userToken.id),
// token: userToken.token,
// // latitude: '<%= latitude %>',
// // longitude: '<%= longitude %>'
// },
// Use locationTemplate to send exact URL-encoded format
// locationTemplate: '{"latitude":"<%= latitude %>","longitude":"<%= longitude %>"}',
// extras: {
// user_id: String(userToken.id),
// token: userToken.token,
// },
httpRootProperty: '.', // Send locationTemplate as root, not nested under "location"
}).then((state) => {
console.log("✅ BackgroundGeolocation configured and ready:", state.enabled);
console.log("- URL:", REPORT_USER_GPS_COORDINATES);
console.log("- User ID:", userToken.id);
BackgroundGeolocation.destroyLocations();
if (!state.enabled) {
BackgroundGeolocation.start(function (state) {
console.log("- Start success:", state);
});
};
BackgroundGeolocation.onLocation(location => {
console.log('[onLocation]', location);
}, error => {
console.warn('[onLocation] ERROR:', error);
});
BackgroundGeolocation.onMotionChange(event => {
console.log('[onMotionChange]', event);
});
BackgroundGeolocation.onActivityChange(event => {
console.log('[onActivityChange]', event);
});
BackgroundGeolocation.onProviderChange(event => {
console.log('[onProviderChange]', event);
});
BackgroundGeolocation.onGeofence(event => {
console.log('[onGeofence]', event);
});
BackgroundGeolocation.onConnectivityChange(event => {
console.log('[onConnectivityChange]', event);
});
BackgroundGeolocation.onEnabledChange(enabled => {
console.log('[onEnabledChange]', enabled);
});
BackgroundGeolocation.onHttp(event => {
console.log('[onHttp]', event);
});
BackgroundGeolocation.onPowerSaveChange(enabled => {
console.log('[onPowerSaveChange]', enabled);
});
BackgroundGeolocation.onSchedule(state => {
console.log('[onSchedule]', state);
});
BackgroundGeolocation.onAuthorization(event => {
console.log('[onAuthorization]', event);
});
BackgroundGeolocation.onHeartbeat(async event => {
console.log('[onHeartbeat]', event);
await reportLocationCoordinates({ latitude: event.location.coords.latitude, longitude: event.location.coords.longitude })
.then(data => {
console.log('reportLocationCoordinates response: ', data);
return data;
})
.catch(error => {
console.error(error);
return null;
});
});
}).catch((error) => {
console.error("❌ BackgroundGeolocation configuration error:", error);
});
} else {
console.error("❌ No user token found - cannot initialize BackgroundGeolocation");
};
};
initBackgroundGeolocationService();
return () => {
if (onHttp?.remove) {
onHttp.remove();
}
console.log("-----------BackgroundGeolocationService destroyed---------------");
};
}, []);
Required Reading
Plugin Version
4.19.3
Mobile operating-system(s)
Device Manufacturer(s) and Model(s)
Samsung A55
Device operating-systems(s)
Android 15
React Native / Expo version
0.83.0
What happened?
I have configured the package to send HTTP requests to the given API every 60 seconds (fastest interval for testing purposes), but it is never sending anything. I have also tried it with locationTemplate (see commented code), but the same issue remains.
As a final resort, I decided to go with heartbeatInterval, but even that is not working as it should. It does not trigger every 60 seconds as is specified, but at irregular intervals (over 5 minutes).
Plugin Code and/or Config
Relevant log output