transistorsoft / capacitor-background-geolocation

The most sophisticated background location-tracking & geofencing module with battery-conscious motion-detection intelligence for iOS and Android.
MIT License
96 stars 16 forks source link

Overwhelming Amount of DUPLICATE onLocation Events #38

Closed josiahbryan closed 2 years ago

josiahbryan commented 2 years ago

Your Environment

Latest Dependencies:

@capacitor/cli: 3.3.3 @capacitor/core: 3.3.3 @capacitor/android: 3.3.3 @capacitor/ios: 3.3.3

Installed Dependencies:

@capacitor/cli: 3.1.2 @capacitor/core: 3.1.2 @capacitor/android: 3.1.2 @capacitor/ios: 3.1.2

[success] iOS looking great! πŸ‘Œ [success] Android looking great! πŸ‘Œ

* Plugin config provided to `#ready`:
```javascript <!-- syntax-highlighting:  paste your code below -->
{
    // Geolocation Config
    desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
    distanceFilter: 1,
    preventSuspend: true,
    // Activity Recognition
    stopTimeout: 1,
    // Application config
    debug: false, // <-- enable this hear sounds for background-geolocation life-cycle.
    logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
    stopOnTerminate: false, // <-- Allow the background-service to continue tracking when user closes the app.
    startOnBoot: false, // <-- Auto start tracking when device is powered-up.
    // HTTP / SQLite config
    batchSync: false, // <-- [Default: false] Set true to sync locations to server in a single HTTP request.
    autoSync: true, // <-- [Default: true] Set true to sync each location to server as it arrives.
    extras: {
        // <-- Optional meta-data appended to each recorded location.
        sensor,
        // For correlating on the server to a given device and via datadog
        clientTransactionId: `${transactionPrefix}.native`,
    },
    url: `${urlRoot}/api/v1/store-gps-location`,
    // syncUrl: `${server.urlRoot}/api/v1/store-gps-location`,
    // syncThreshold: 1,
    headers: {
        // We should have token by now because
        // connectNativeLogger() only called by LOGIN_EVENT
        Authorization: server.token,
    },

    foregroundService: true,
    notification: {
        channelName: 'Trip GPS',
        title: 'Vaya is using your location',
        text: 'Safety and enhanced ride experience are important to Vaya',
        //   "Vaya uses your location while you're wait ing for a ride or riding in the car in order to keep you safe and enhance your ride experience",
        smallIcon: 'drawable/push_icon',
        // largeIcon: 'drawable/push_icon',
        color: `#${MAP_BRAND_COLOR_RAW_HEX}`,
        // Notification strongly weighted to bottom of list; notification-bar icon hidden
        // https://transistorsoft.github.io/capacitor-background-geolocation/interfaces/notification.html#priority
        priority: BackgroundGeolocation.NOTIFICATION_PRIORITY_MIN,
        // TODO: Custom layout? https://transistorsoft.github.io/capacitor-background-geolocation/interfaces/notification.html#layout
        // https://github.com/transistorsoft/capacitor-background-geolocation/wiki/Android-Custom-Notification-Layout
    },
    // Android 11+ requires:
    locationAuthorizationRequest: 'Always',
    backgroundPermissionRationale: {
        title:
            "Allow {applicationName} to access to this device's location in the background?",
        message:
            'In order to accurately connect drivers with members and to ensure your safety in the background, please enable {backgroundPermissionOptionLabel} location permission',
        positiveAction: 'Change to {backgroundPermissionOptionLabel}',
        negativeAction: 'Cancel',
    },
    // // customize post properties
    // postTemplate: {
    //  lat: '@latitude',
    //  lng: '@longitude',
    //  accuracy: '@accuracy',
    //  // Added to attempt server-side walk/run/drive detection
    //  speed: '@speed',
    //  altitude: '@altitude',
    //  bearing: '@bearing',
    //  // built above from deviceInfo()
    //  sensor,
    // },
}

Expected Behavior

One onLocation event per UUID (unless previous onLocation for same UUID threw an Error)

Actual Behavior

Hundreds of onLocation events per UUID

Steps to Reproduce

  1. Run the app on iOS
  2. ....?
  3. Examine logs
  4. Cry

Note: I've only seen this in production / "in the wild" - cannot reproduce locally or in dev.

This screenshot shows console logs captured via LogRocket. Happy to provide access to LogRocket sessions showing this issue:

image

These onLocation events are foreground events, NOT background iOS events. They are being captured and logged by the following code (sanatized):

BackgroundGeolocation.onLocation(
   (location) => {
      console.log('[BackgroundGeolocation.onLocation]', location);
      // ...
   }
);

Context

Track drivers

Debug logs

Logs not available from production devices

Logs ``` PASTE_YOUR_LOGS_HERE ```
christocracy commented 2 years ago

You are probably adding multiple onLocation event-listeners.

See API docs Subscription

josiahbryan commented 2 years ago

Are those not automatically removed when stopping the background plugin? Do we have to remove all listeners when calling stop?

christocracy commented 2 years ago

remove all listeners when calling stop?

The plugin doesn’t emit events when stopped.

every time you call onLocation, a new listener is created.

christocracy commented 2 years ago

Are those not automatically removed when stopping the background plugin?

no. When you turn off your stereo, music stops playing from the speakers. The speaker wires don’t get disconnected. Every time you call onLocation, it’s like wiring up a new speaker to your stereo system.

josiahbryan commented 2 years ago

@christocracy something's wrong - I'm getting undefined as the return onLocation:

const subscription = BackgroundGeolocation.onLocation(location => {
  console.log("[onLocation] ", location);
});
console.log(`trackSubscription`, subscription);

image (3x instances of the message because I start/stop the plugin in response to user enabling/disabling "tracking" at various points in the UI)

I thought it might be plugin version, but I updated to the latest plugin (4.4.3) and no change - still get undefined - how am I supposed to stop listening to subscriptions if I don't get a subscription? :-D haha!

image

Suggestions...?

christocracy commented 2 years ago

You probably didn't npx cap sync after installing.

josiahbryan commented 2 years ago

Hmm...well, my postinstall script does do npx cap sync, but I suppose something didn't sync right because a few hours later (now) after doing npx cap sync again, it suddenly shows a proper return value. All good now. Thanks.