flutter_background_geolocation icon indicating copy to clipboard operation
flutter_background_geolocation copied to clipboard

Send the information to 2 different servers

Open omejiasq opened this issue 2 years ago • 4 comments

I have no errors in my code, I just simply want to know what is the best option to send the information collected by the plugin to 2 servers with different IPs, and/or whether to call the "bg.BackgroundGeolocation.ready" event twice like I explain it below, is it correct?

Your Environment

  • Plugin version:4.12.2
  • Platform: iOS or Android:Android
  • OS version:13
  • Device manufacturer / model:Samsung galaxy s21 ulta
  • Flutter info (flutter doctor):
  • Plugin config:
PASTE_YOUR_CODE_HERE

Expected Behavior

send the information to 2 different servers or ips

Actual Behavior

Send only the info to one server or IP platform

Steps to Reproduce

1. 2. 3. 4.

Context

call 2 times this method:"ready" in the code. I have a similar implementation as the default documentation a examples of Transistorsoft --first parte of my code in _initPlatformState method in flutter (myip1): bg.BackgroundGeolocation.ready(bg.Config( reset: true, debug: false, //habilitar efectos de sonido y notificaciones locales logLevel: bg.Config.LOG_LEVEL_VERBOSE, desiredAccuracy: bg.Config.DESIRED_ACCURACY_HIGH, distanceFilter: 10.0, 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 enable recording your trips to work and calculate distance-travelled.", positiveAction: 'Change to "{backgroundPermissionOptionLabel}"', negativeAction: 'Cancel'), url: "http://myip1/device/locations2", authorization: bg.Authorization( strategy: bg.Authorization.STRATEGY_JWT, accessToken: 'QQQ', ), extras: { "smartphone_blocked": _isBlocked, "tipos_viajes": tipos_viajes, "company" : company, "ayuda" : ayuda, },
stopOnTerminate: false, startOnBoot: true, enableHeadless: true)) .then((bg.State state) { print("[ready] ${state.toMap()}"); setState(() { _enabled = state.enabled; _isMoving = state.isMoving!; }); }).catchError((error) { print('[ready] ERROR: $error'); }); }

--After the previous code I call this one only by changing the IP to another server as follows: bg.BackgroundGeolocation.ready(bg.Config( reset: true, debug: false, //habilitar efectos de sonido y notificaciones locales logLevel: bg.Config.LOG_LEVEL_VERBOSE, desiredAccuracy: bg.Config.DESIRED_ACCURACY_HIGH, distanceFilter: 10.0, 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 enable recording your trips to work and calculate distance-travelled.", positiveAction: 'Change to "{backgroundPermissionOptionLabel}"', negativeAction: 'Cancel'), url: "http://myip2/device/locations2", authorization: bg.Authorization( strategy: bg.Authorization.STRATEGY_JWT, accessToken: 'QQQ', ), extras: { "smartphone_blocked": _isBlocked, "tipos_viajes": tipos_viajes, "company" : company, "ayuda" : ayuda, },
stopOnTerminate: false, startOnBoot: true, enableHeadless: true)) .then((bg.State state) { print("[ready] ${state.toMap()}"); setState(() { _enabled = state.enabled; _isMoving = state.isMoving!; }); }).catchError((error) { print('[ready] ERROR: $error'); }); }

Debug logs

Logs
PASTE_YOUR_LOGS_HERE

omejiasq avatar Dec 15 '23 19:12 omejiasq

You cannot send to two different servers and you cannot achieve that by calling .ready two times. The plug-in is a singleton with only one instance.

Have server A send the data to server B. it would be a power drain to send data to two servers from the mobile device.

christocracy avatar Dec 15 '23 20:12 christocracy

thank you very much Chris!

omejiasq avatar Dec 15 '23 20:12 omejiasq

@omejiasq You can take advantage of BackgroundGeolocation.onLocation - listen to those events then send whatever you want to your endpoint - in our case a Firebase function:

  BackgroundGeolocation.onLocation(
    (Location location) async {
      final state = await BackgroundGeolocation.state;
      trackerApi.log({
        'event': '[onLocation] success',
        'data': location.toString(),
        'state': state.map,
      });
    },
    (LocationError error) {
      trackerApi.log(
        {
          'event': '[onLocation] ERROR',
          'data': error.toString(),
        },
      );
    },
  );

xni06 avatar Apr 12 '24 12:04 xni06

Yes, of course. You’re free to do whatever you wish in event-listeners.

christocracy avatar Apr 12 '24 12:04 christocracy