-
Notifications
You must be signed in to change notification settings - Fork 441
Open
Labels
Description
Required Reading
- Confirmed
Plugin Version
4.19.0
Mobile operating-system(s)
- iOS
- Android
Device Manufacturer(s) and Model(s)
Armor 20WT
Device operating-systems(s)
android 12
React Native / Expo version
0.80.2 react native
What do you require assistance about?
I have purchase starter packages I’m experiencing an issue with polygon geofence detection. The location tracking is working perfectly in both debug and release builds on Android. However, the polygon geofence enter/exit events are not being triggered in the release build, even though they work as expected in debug mode.
[Optional] Plugin Code and/or Config
useEffect(() => {
activeShiftRef.current = activeShift;
if (isInitialized.current) {
return;
}
if (
isClockedIn !== true &&
activeShiftRef?.current?.siteId?.userTrack !== 'active'
) {
return;
}
isInitialized.current = true;
const initLocation = async () => {
await BackgroundGeolocation.requestPermission().then(status => {
console.log('Permission status:', status);
});
await BackgroundGeolocation.ready(
{
desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_LOW,
stopTimeout: 1,
distanceFilter: 30,
stopOnTerminate: false,
disableMotionActivityUpdates: false,
startOnBoot: true,
locationAuthorizationRequest: 'Always',
preventSuspend: true,
showsBackgroundLocationIndicator: true,
foregroundService: true,
notification: {
title: 'Shift Tracking',
text: 'Location tracking active',
color: '#4CAF50',
priority: BackgroundGeolocation.NOTIFICATION_PRIORITY_HIGH,
},
},
state => {
console.log('[ready] state:', state);
if (
!state.enabled &&
isClockedIn === true &&
activeShiftRef?.current?.siteId?.userTrack === 'active'
) {
BackgroundGeolocation.start();
isTrackingStarted.current = true;
console.log('Background tracking started.');
}
},
);
await BackgroundGeolocation.onLocation(async location => {
const state = await BackgroundGeolocation.getProviderState();
if (!state.enabled) {
return;
}
if (!isClockedIn) {
return;
}
if (
location?.coords?.latitude !== undefined &&
location?.coords?.longitude !== undefined
) {
const now = Date.now();
const trackingIntervalMin = activeShiftRef?.current?.siteId?.trackingLogTimerMinute || radarTrackingTime / 60;
const trackingIntervalMs = trackingIntervalMin * 60 * 1000;
if (now - lastLocationTime.current < trackingIntervalMs) {
return;
}
lastLocationTime.current = now;
const stopped = location?.is_moving;
const shouldSend =
isStoppedRef.current === null ||
(isStoppedRef.current === true && stopped === false) ||
(isStoppedRef.current === false && stopped === false) ||
(isStoppedRef.current === false && stopped === true);
if (shouldSend) {
await setLastUserTrack({ ...location });
await geofanceTrack(location);
await delay(5000);
}
}
});
};
if (
isClockedIn &&
activeShiftRef?.current?.siteId?.userTrack === 'active'
) {
initLocation();
}
return () => {
if (isTrackingStarted.current) {
isTrackingStarted.current = false;
isInitialized.current = false;
BackgroundGeolocation.removeAllListeners();
}
};
}, [isClockedIn]);
useEffect(() => {
const addGeofence = async () => {
if (
!geoFancingData?.length ||
!isClockedIn ||
activeShiftRef?.current?.siteId?.userTrack !== 'active'
)
return;
const state = await BackgroundGeolocation.getProviderState();
if (!state.enabled) {
return;
}
if (!isClockedIn) return;
const geofences = geoFancingData?.map(item => ({
identifier: item.loactionName,
notifyOnEntry: true,
notifyOnExit: true,
vertices: item.fencingBound.map(p => [
parseFloat(p.lat),
parseFloat(p.lng),
]),
}));
BackgroundGeolocation.getGeofences().then(existing => {
const existingIds = existing.map(g => g.identifier);
const toAdd = geofences.filter(
g => !existingIds.includes(g.identifier),
);
if (toAdd?.length > 0) {
BackgroundGeolocation.addGeofences(toAdd)
.then(() => console.log(`:white_tick: Added ${toAdd.length} new geofences`))
.catch(err => console.log(':x: Geofence error:', err));
} else {
console.log(':zap: Geofences already registered, skipping add');
}
});
};
addGeofence();
const appStateListener = AppState.addEventListener(
'change',
nextAppState => {
if (
nextAppState === 'active' &&
isClockedIn &&
activeShiftRef?.current?.siteId?.userTrack === 'active'
) {
addGeofence();
}
},
);
return () => {
appStateListener.remove();
};
}, [geoFancingData, isClockedIn, activeShift]);
useEffect(() => {
if (!isClockedIn && activeShiftRef?.current?.siteId?.userTrack !== 'active')
return;
const geofenceSubscription = BackgroundGeolocation.onGeofence(
async geofence => {
const now = Date.now();
if (now - lastLogTime.current < 60000) {
return;
}
lastLogTime.current = now;
const geofenceId = geoFancingDataRef?.current?.find(
item => item?.loactionName === geofence?.identifier,
)?._id;
if (
(geofence?.action == 'ENTER' && lastTrackLog === null) ||
lastTrackLog === 'GEOFENCE_EXIT'
) {
await setLastTrackLog('GEOFENCE_ENTER');
await geofanceTrack(geofence?.location, 'GEOFENCE_ENTER', geofenceId);
await delay(5000);
} else if (
geofence?.action == 'EXIT' &&
lastTrackLog === 'GEOFENCE_ENTER'
) {
await setLastTrackLog('GEOFENCE_EXIT');
await geofanceTrack(geofence?.location, 'GEOFENCE_EXIT', geofenceId);
await delay(5000);
}
},
);
return () => {
geofenceSubscription?.remove();
};
}, [isClockedIn, lastTrackLog, activeShiftRef]);