flutter_background_geolocation icon indicating copy to clipboard operation
flutter_background_geolocation copied to clipboard

Initial Geofence events missed for IOS handlers when the app is launched as a service form the background

Open stevegaunt opened this issue 3 years ago • 6 comments

Your Environment

  • Plugin version: 4.7.0

  • Platform: IOS

  • OS version: 15.5

  • Device manufacturer / model: Iphone 12 mini

  • Flutter info (flutter doctor): [✓] Flutter (Channel stable, 2.8.1, on macOS 12.4 21F79 darwin-x64, locale en-GB) [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0) [✓] Xcode - develop for iOS and macOS (Xcode 13.4.1) [✓] Chrome - develop for the web [!] Android Studio (not installed) [✓] IntelliJ IDEA Ultimate Edition (version 2021.2) [✓] Connected device (3 available)

  • Plugin config:



bg.Config(
        // tracking turning off issue is present on both android and ios: we don't want to set preventSuspend to true because it will increase our battery drain dramatically
        // preventSuspend: true,
        // heartbeatInterval: 60,
        autoSync: true,
        autoSyncThreshold: 20,
        desiredAccuracy: Config.DESIRED_ACCURACY_HIGH,
        distanceFilter: 50.0,
        elasticityMultiplier: 4.0,
        geofenceModeHighAccuracy: true,
        stopOnTerminate: false,
        startOnBoot: true,
        logLevel: Config.LOG_LEVEL_INFO,
        logMaxDays: 1,
        reset: kDebugMode,
        debug: kDebugMode,
        locationAuthorizationRequest: 'Always',
        enableHeadless: true,
        url: Config.LOCATION_API_URL,
        headers: headers,
        batchSync: true,
        maxBatchSize: 500,
        maxRecordsToPersist: 100000,
        maxDaysToPersist: 7,
        backgroundPermissionRationale: bg.PermissionRationale(
            title: "Allow {applicationName} to access this device's location even when closed or not in use?",
            message: "This enables the automatic logging",
            positiveAction: "Change to \"{backgroundPermissionOptionLabel}\"",
            negativeAction: ""));
  }


 bg.onGeofence((GeofenceEvent event) {
  print('[onGeofence] ${event}');
});

Expected Behavior

The geofence exit event correctly starts the IOS app as a service. the call-back for the bg.onGeofence should get the initial geofence events that initiated starting the IOS app as a service

Actual Behavior

The initial geofence event doesn't get handled, only additional geoevents from once the app as been started as a service gets handled.

Steps to Reproduce

  1. move to a geofence location
  2. terminate the app
  3. leave the geofence
  4. the geofence event doesn't get handled.

Context

We need to be able to know when geofences get entered/exit even if the app has been terminated.

Debug logs

Logs
PASTE_YOUR_LOGS_HERE

stevegaunt avatar Jul 21 '22 10:07 stevegaunt

Register event-listeners before calling .ready(), not after.

christocracy avatar Jul 21 '22 10:07 christocracy

@christocracy there currently are.

bg.BackgroundGeolocation.onGeofence((bg.GeofenceEvent event) async {

   log('[geofence] ${event.identifier}, ${event.action}');
});
bg.BackgroundGeolocation.onGeofencesChange((bg.GeofencesChangeEvent event) async {

  log('[GeofencesChangeEvent off] ${event.off}');
 log('[GeofencesChangeEvent on] ${event.on}');
});
//this config is **only** used for the first install and is ignored thereafter. It becomes the default.
var locationTrackingConfig = await sdk.getBaseLocationConfig(deviceInfoHeadersFactory);

try {
  await  bg.BackgroundGeolocation.ready(locationTrackingConfig);
} catch (error, stack) {
  CrashReporting.instance.recordError(error, stack);
}

stevegaunt avatar Jul 21 '22 10:07 stevegaunt

I've been running the /example app for years on my main iOS device (currently iPhone 13 Pro @ 15.5). I constantly monitor about 15 geofences throughout my neighbourhood.

I just took a little bike ride.

  • After I enter each geofence, I terminate the app.
  • After riding out of each geofence, I open app to see if the geofence event is printed on the map (the /example app renders geofence markers on the map with the onGeofence event.

I am not able to reproduce the issue "Initial Geofence events missed for IOS handlers when the app is launched as a service form the background".

Opening the app after riding out of a geofence:

  • onGeofence renders markers to highlight a geofence event.
  • red dot denotes EXIT event.
  • blue dot = location where event occurred.
  • If somehow onGeofence did not fire after re-launching in background, the markers would not be rendered.

IMG_1811

Posted events to demo server:

  • Geofence circled red is the geofence from screenshot above:
Screen Shot 2022-07-21 at 4 00 08 PM

christocracy avatar Jul 21 '22 20:07 christocracy

Hello @christocracy, I need to track geofence event action in the terminate state of the app. In IOS it's not working, I have also tried preventSuspend true in geofence config. Please help me with that. Thanks in advance!

Software Version: 15.3.1 Device: iPhone X

iphonedev4radixweb avatar Aug 05 '22 08:08 iphonedev4radixweb

I have also tried preventSuspend tru

This has nothing to do with geofence firing.

Software Version: 15.3.1

This os version is know to have bugs. Upgrade to latest.

christocracy avatar Aug 05 '22 08:08 christocracy

Okay, then I will not use preventSuspend & let me try in another version. I will get back to you. Thanks!

iphonedev4radixweb avatar Aug 05 '22 09:08 iphonedev4radixweb