flutter_background_geolocation icon indicating copy to clipboard operation
flutter_background_geolocation copied to clipboard

[Help Wanted]: The Geofence EXIT event is triggered after force quitting and then reopening in iOS.

Open gtran-dev opened this issue 8 months ago • 13 comments

Required Reading

  • [x] Confirmed

Plugin Version

4.16.9

Mobile operating-system(s)

  • [x] iOS
  • [ ] Android

Device Manufacturer(s) and Model(s)

iPhone 14 Pro

Device operating-systems(s)

iOS 18.4.1

What do you require assistance about?

Hi @christocracy ,

I'd like to reopen the issue that was already discussed in this ticket. https://github.com/transistorsoft/flutter_background_geolocation/issues/1477

I have problem on iOS, I enter the zone and receive the ENTER event. Then I close the app in the background and reopen it. The EXIT event is generated immediately, even if I'm still in the polygon. In the logs, I see that the plugin keeps throwing this error after calling ready(). do you have any ideas?

I've attached the logs from "flutter logs" and .emailLog.

I've already run three tests in the logs.

flutter-logs.txt background-geolocation.log

[Optional] Plugin Code and/or Config

bg.BackgroundGeolocation.ready(bg.Config(
          reset: true,
          enableHeadless: true,
          foregroundService: true,
          desiredAccuracy: bg.Config.DESIRED_ACCURACY_HIGH,
          isMoving: true,
          stationaryRadius: 25,
          distanceFilter: 10.0,
          notification: bg.Notification(
              smallIcon: "drawable/launcher_icon",
              largeIcon: "mipmap/launcher_icon",
              sticky: false,
              title: "$title - tracking your location",
              text:
              "$title accessing background location to track your location, even when it's not running."),
          backgroundPermissionRationale: bg.PermissionRationale(
              title: "Allow {applicationName} to access this device's location even when the app is closed or not in use.",
              message: "This app collects location data to change your status based on geofence.",
              positiveAction: 'Change to "{backgroundPermissionOptionLabel}"',
              negativeAction: 'Cancel'
          ),
          stopOnTerminate: false,
          startOnBoot: true,
          debug: false,
          heartbeatInterval: 60,
          preventSuspend: false,
          persistMode: bg.Config.PERSIST_MODE_ALL,
          geofenceModeHighAccuracy: true,
          showsBackgroundLocationIndicator: true,
          maxDaysToPersist: 1,
          locationAuthorizationRequest: 'Always',
          logLevel: bg.Config.LOG_LEVEL_VERBOSE))
          .then((bg.State state) {
        print('[ready] - $state');
        print('!!!!!!!!!!!!!!!     start background geolocation with forceRestart: $forceRestart and enableDebugMode: $enableDebugMode');
        if (enableDebugMode) {
          startDebugMode();
        } else {
          if (!state.enabled || forceRestart) {
            // start the plugin
            bg.BackgroundGeolocation.startGeofences();
          }
        }
      });

[Optional] Relevant log output


gtran-dev avatar May 13 '25 15:05 gtran-dev

Provide the details of the polygon you’re using.

christocracy avatar May 13 '25 15:05 christocracy

Polygon: 6§§§company flutter: Add Polygon: [[52.5300849, 13.4108336], [52.5298858, 13.4113177], [52.5305288, 13.4121801], [52.5307246, 13.4117885], [52.5300849, 13.4108336]] [MiniBall] Circle coordinate: 52.530307, 13.411507, radius: 78.825155 [MiniBall] squared_radius: 0.000001, corrected radius: 78.825155

Polygon: 8§§§Test Place flutter: Add Polygon: [[52.5700051, 13.5201411], [52.5700867, 13.5211604], [52.5715212, 13.5211389], [52.5729297, 13.5209457], [52.5729085, 13.5200204], [52.5717657, 13.519712], [52.5700051, 13.5201411]] [MiniBall] Circle coordinate: 52.571476, 13.520536, radius: 169.328563 [MiniBall] squared_radius: 0.000002, corrected radius: 169.328563

Polygon: 9§§§SHA Home flutter: Add Polygon: [[52.5378207, 13.520823], [52.5372554, 13.5212186], [52.537297, 13.5213782], [52.5378672, 13.5209732], [52.5378207, 13.520823]] [MiniBall] Circle coordinate: 52.537559, 13.521101, radius: 42.433240 [MiniBall] squared_radius: 0.000000, corrected radius: 42.433240

Polygon:: 14§§§Home flutter: Add Polygon: [[52.5719479, 13.5158699], [52.5712437, 13.5151242], [52.5707547, 13.5172968], [52.5714524, 13.5177903], [52.5719479, 13.5158699]] [MiniBall] Circle coordinate: 52.571348, 13.516457, radius: 148.681847 [MiniBall] squared_radius: 0.000002, corrected radius: 148.681847

gtran-dev avatar May 13 '25 15:05 gtran-dev

I cannot reproduce this with the /example app in this repo.

Please create a simple Hello World app for me to install, which reproduces this.

After receiving ENTER event, I terminate and restart the app. Restarting the app, I do NOT receive an EXIT event. Viewing logs, I see:

╔═══════════════════════════════════════════════════════════
║ -[PolygonGeofencingService isInPolygon:] 📍 45.518865, -73.600513, acy: 12.9m
╚═══════════════════════════════════════════════════════════
ℹ️-[PolygonGeofencingService isInPolygon:] --> home: 100.0%

╔═══════════════════════════════════════════════════════════
║ -[PolygonGeofencingService isInPolygon:] 📍 45.518865, -73.600513, acy: 11.2m
╚═══════════════════════════════════════════════════════════
ℹ️-[PolygonGeofencingService isInPolygon:] --> home: 100.0%

╔═══════════════════════════════════════════════════════════
║ -[PolygonGeofencingService isInPolygon:] 📍 45.518865, -73.600513, acy: 14.4m
╚═══════════════════════════════════════════════════════════
ℹ️-[PolygonGeofencingService isInPolygon:] --> home: 100.0%

╔═══════════════════════════════════════════════════════════
║ -[PolygonGeofencingService isInPolygon:] 📍 45.518865, -73.600513, acy: 14.6m
╚═══════════════════════════════════════════════════════════
ℹ️-[PolygonGeofencingService isInPolygon:] --> home: 100.0%

╔═══════════════════════════════════════════════════════════
║ -[PolygonGeofencingService isInPolygon:] 📍 45.518865, -73.600513, acy: 11.9m
╚═══════════════════════════════════════════════════════════
ℹ️-[PolygonGeofencingService isInPolygon:] --> home: 100.0%

╔═══════════════════════════════════════════════════════════
║ -[PolygonGeofencingService isInPolygon:] 📍 45.518865, -73.600513, acy: 10.4m
╚═══════════════════════════════════════════════════════════
ℹ️-[PolygonGeofencingService isInPolygon:] --> home: 100.0%

christocracy avatar May 14 '25 14:05 christocracy

You won't believe it. The problem lies in the §§§ symbol I use in the polygon identifier. I tested it in the example app, and it causes the same error logs.

gtran-dev avatar May 14 '25 20:05 gtran-dev

§§§

Why do you use this weird symbol?

christocracy avatar May 14 '25 21:05 christocracy

It’s likely due to naïve string-handling in the C++ code. Don’t use weird symbols for now.

christocracy avatar May 14 '25 22:05 christocracy

I'm using it because I need more information for the geofence callback, and I believe this is the only place I can pass it using this rare symbol to split information. It would be better if we could insert additional data into the polygon and retrieve it in the callback. Maybe you can add validation directly here and in this case raise the error if someone uses an invalid symbol.

Update: Ah, I see you have an extras field. Perfect.

gtran-dev avatar May 15 '25 06:05 gtran-dev

By the way, is there a rule for the name of the polygon identifier? Our customer can change the name as they wish.

gtran-dev avatar May 15 '25 07:05 gtran-dev

Hi @christocracy, sorry that I'm having too many problems. Boot in background only works if debug:true. If debug:false, the app won't boot after force quitting from the background. I also tested it with a example app, and it shows the same behavior.

gtran-dev avatar May 15 '25 12:05 gtran-dev

Boot in background only works if debug:true

Not true. Config.debug has no effect on performance. A terminated iOS will not relaunch until the device moves at least 200 meters.

christocracy avatar May 15 '25 12:05 christocracy

is there a rule for the name of the polygon identifier?

Use any character found on a keyboard.

christocracy avatar May 15 '25 12:05 christocracy

More specifically: If debug:true is set and I immediately quit the app, it immediately boot in the background. I can see this in the logs.

If I first put the app in the background, then open the app switcher and quit the app, it doesn't boot. That's what you mean.

The first case is very useful, though, and only works with debug:true. I want it with debug:false as well. Because I'm already inside the polygon. I force quit the app and step outside the polygon. I have to walk at least 200 meters to reactivate the app (I've tested this case several times, but it doesn't trigger an EXIT event after the app is active again). However, if we can activate the app immediately after quitting, it works perfectly.

I don't know why this behaves differently, but you can test it in the example app.

gtran-dev avatar May 15 '25 13:05 gtran-dev

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Jun 15 '25 02:06 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Jun 29 '25 02:06 github-actions[bot]