react-native-background-geolocation
react-native-background-geolocation copied to clipboard
onGeofence not trigger running
Your Environment
- Plugin version: 4.16.5
- Platform:both ios and android
- OS version: 17
- Device manufacturer / model: 15pro max
- React Native version (
react-native -v):0.71.11 - Plugin config
BackgroundGeolocation.onGeofence(geofence => {
if (__DEV__) {
console.log('-- onGeofences', geofence);
}
writeLog(
`Geos: ${geofence.action}, id: ${geofence.identifier}, address : ${geofence.extras.address}`,
);
onGeofenceHandler(geofence); //do some thing
});
BackgroundGeolocation.ready(
{
// Geolocation Config
transistorAuthorizationToken: token,
autoSync: true,
distanceFilter: 20,
// enableHeadless: true,
// stopOnTerminate: false,
// startOnBoot: true,
backgroundPermissionRationale: {
message:
'Notify Crews of geofence events, even when the app is in the background.',
},
locationAuthorizationRequest: 'Always',
debug: false,
preventSuspend: true,
heartbeatInterval: 60,
logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
desiredAccuracy: Platform.select({
ios: BackgroundGeolocation.DESIRED_ACCURACY_NAVIGATION,
android: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
}),
disableLocationAuthorizationAlert: false,
geofenceProximityRadius: 5000,
disableStopDetection: disableStopDetection,
//Android
locationUpdateInterval: 5000,
geofenceModeHighAccuracy: true,
//ios
pausesLocationUpdatesAutomatically: false,
reset: false,
},
state => {
// if (!state.enabled) {
BackgroundGeolocation.start(function () {
BackgroundGeolocation.changePace(true);
if (__DEV__) {
console.log('- Start success');
}
});
// }
},
);
BackgroundGeolocation.addGeofence({
notifyOnEntry: true,
notifyOnExit: true,
identifier: "Home"
vertices: [
[10.803200934228789, 106.64657450468907],
[10.802926928253568, 106.65380574019322],
[10.797973700209688, 106.6537842825211],
[10.79812124435301, 106.64668179304967],
[10.803200934228789, 106.64657450468907],
],
})
.then(success => {
if (__DEV__) {
console.log('[addGeofence] success', success, geofence);
}
})
.catch(error => {
sentry.captureException(error);
});
Expected Behavior
I'm running in a polygon but it don't trigger running Function onGeofence will trigger running
Actual Behavior
Function onGeofence will not trigger running
See wiki “Debugging”. Learn to observe the plug-in logs.
The first thing to do when something unexpected happens is use debug: true and observe the plug-in’s incredibly verbose logs.
@christocracy I already did that, I work fine with the normal lat, long
identifier: Home
latitude: property.latitude,
longitude:property.longitude,
radius: radiusJob,
but with the polygon it not trigger
Analyze your polygon shape. Plot the points on a map. The order of vertices matters.
@christocracy this is my polygon, my location is in the center of the polygon as you can see in the picture, but the onGeofence not work, all polygon was add success and already on in onGeofencesChange
Your polygon has 5 vertices. That doesn’t look like any kind of pentagon.
The order of vertices matters. For example, making a square in the SampleApp:
Use your map api to draw a polygon using your vertices.
@christocracy I try using a square like you said, but it still not trigger running
vertices: [
[10.802129317156131, 106.64898325473482],
[10.801897465135191, 106.6522233632248],
[10.798862294898008, 106.65190149814302],
[10.799073071599935, 106.64867211848909],
],
Go outside and walk out of your polygon then walk back in. Use debug: true so you can hear the debug soundFX.
reset: false,
Why are you using this? If you don’t know what it does, don’t use it.
This issue is stale because it has been open for 30 days with no activity.
This issue was closed because it has been inactive for 14 days since being marked as stale.
I have same problem. The onGeofence listener is working when using radius, latitude, and longitude, but it does not work when using vertices as documented here
Maybe there’s a problem with your vertices / polygon too small. I have no idea since you’ve not provided any information. See the original post here to see the type of information required.
Your Environment
- Plugin version: 4.14.5
- Platform: iOS
- OS version: 17.5
- Device manufacturer / model: iPhone SE (3rd generation) - Simulator
- React Native version (
react-native -v): 0.72.8 - Plugin config
BackgroundGeolocation.addGeofence({
identifier: 'ZONE_OF_INTEREST',
notifyOnExit: true,
notifyOnEntry: true,
vertices: [
[25.28215452768285, 51.51194885222792],
[25.27718742129699, 51.51362255065321],
[25.279166527118864, 51.51782825438856],
[25.283124641909716, 51.515768317865124],
],
});
// Configure the plugin
BackgroundGeolocation.ready({
desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
startOnBoot: true,
stopOnTerminate: false,
autoSync: true,
logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
debug: true,
}).then(state => {
BackgroundGeolocation.startGeofences();
});
BackgroundGeolocation.onGeofence(event => {
const {identifier, action} = event;
if (action === 'ENTER') {
log.debug(`Entered geofence: ${identifier}`);
} else if (action === 'EXIT') {
log.debug(`Exited geofence: ${identifier}`);
}
});
Expected Behavior
Should trigger onGeofence event and log if entered or exited the geofence
Actual Behavior
onGeofence not triggered and no logs are showing.
Context
I'm trying to simulate 'Freeway drive' to exist the geofence, and setting the location again inside the geofence but the onGeofence is not triggered, I tried using the radius instead of vectors and it works as expected. I Also, saw the logs in Flipper and xCode and they show that geofence is entered and existed, but not firing the onGeofence.
Debug logs
Plugin version: 4.14.5
That version is nearly a year old. See CHANGELOG and update to latest version: https://github.com/transistorsoft/react-native-background-geolocation/blob/master/CHANGELOG.md
Plugin version: 4.14.5
That version is nearly a year old. See CHANGELOG and update to latest version: https://github.com/transistorsoft/react-native-background-geolocation/blob/master/CHANGELOG.md
I updated to the latest version (4.17.1), but I'm getting same behaviour.
Update
I Found that it is working as expected on background only. When the app on foreground it stops the listener for some reason.
I noticed also that there are resetStopTimeout and beginStopTimeout logs after startMonitoring, I don't know if thats relevant.
I Found that it is working as expected on background only. When the app on foreground it stops the listener for some reason.
What specifically is not working? Are you hearing the plug-in debug soundFX?
I noticed also that there are resetStopTimeout and beginStopTimeout logs after startMonitoring, I don't know if thats relevant.
Normal and irrelevant.
What specifically is not working? Are you hearing the plug-in debug soundFX?
The onGeofence event is not firing in the case of using vertices on foreground, also the plug-in debug soundFX of exiting and entering the geofence is not working in that case.
The plugin does not operate any differently in the foreground vs background.
I suggest you test in the iOS Simulator with location simulated with "Freeway Drive".
I suggest you test in the iOS Simulator with location simulated with "Freeway Drive".
Thats what I'm doing
The plugin does not operate any differently in the foreground vs background.
That is what happing with my case, I tested while the app on background, I found that the onGeofence is working as accepted, but when I return to the foreground it stops.
I just test the demo app in the simulator with Freeway drive placing a polygon a few hundred meters from the location "Apple". The onGeofence events for both ENTER and EXIT fire in the foreground. I hear the debug soundFX. I see the green/red exit markers drawn upon the map.
The plugin's behaviour is NOT affected by being in the foreground vs background.
I suggest testing in the React-Native environment with the configuration I provided to reproduce the issue since there might be differences between React-Native and Flutter.
To demonstrate further the issue, please notice the following debug logs in foreground vs background:
https://github.com/user-attachments/assets/1dd395e9-86c0-4cd6-9060-eeeb95569c2d
The above logs demonstrate testing exiting and entering the polygon in the simulator with Freeway drive in the foreground. It shows the app entered the foreground. Then, after the first EXIT, the plugin logs fireGeofenceEvent, but the next ENTER and EXIT actions don't fire the geofence event, and the plugin does not log fireGeofenceEvent.
https://github.com/user-attachments/assets/2f9af936-ba67-493a-9f97-7d5e763c14b2
The above logs demonstrate the app entering the background. The first EIXT does not fire the geofence, but it starts to fire after that, and you can notice the fireGeofenceEvent logs.
It doesn't matter if it's Flutter or React Native. It's the same native libraries running.
Nevertheless, here's a screen-recording of the React Native demo app, running with .startGeofences() in the foreground. The red/green markers are rendered with an .onGeofence event-listener, proving the events fire in the foreground.
╔═══════════════════════════════════════════════════════════
║ -[TSGeofenceManager fireGeofenceEvent:] 📢ENTER Geofence: aaa
╚═══════════════════════════════════════════════════════════
✅-[TSGeofenceManager fireGeofenceEvent:] INSERT: 0C41E3C9-D707-4965-816F-120A8892129A
.
.
.
╔═══════════════════════════════════════════════════════════
║ -[TSGeofenceManager fireGeofenceEvent:] 📢EXIT Geofence: aaa
╚═══════════════════════════════════════════════════════════
✅-[TSGeofenceManager fireGeofenceEvent:] INSERT: 87FA4350-3CFB-43EF-BD99-9587505B091C
https://github.com/user-attachments/assets/8d9ac8d5-eab5-449b-8d98-b05243b5210b
I just test the demo app in the simulator with Freeway drive placing a polygon a few hundred meters from the location "Apple". The
onGeofenceevents for bothENTERandEXITfire in the foreground. I hear the debug soundFX. I see the green/red exit markers drawn upon the map.
I did the test, as you said, and it worked. It turned out that the issue appeared because of the way of simulation. In my case, I was setting the polygon around my current location (somewhere in Asia) and setting my location inside that polygon, and then I simulated the "freeway drive", which changed my location instantly to California, USA, and I expected that it would fire geofence. Nonetheless, my way of simulating caused the onGeofence event to fire when using radios or using vertices in the background as expected, but it does not fire in the case of using vertices in the foreground. I am not sure why it behaves like this in that case, but I'm satisfied using the same simulation as you mentioned since it is more realistic.
I just wanted to share this experience so you can resolve any issues that exist and help anyone facing the same issue.
Place your polygon in California, not Asia, exactly where you see I place mine in the screenshot.