Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Help Wanted]: Location Template not being applied #1420

Open
3 tasks done
precrati opened this issue Feb 4, 2025 · 5 comments
Open
3 tasks done

[Help Wanted]: Location Template not being applied #1420

precrati opened this issue Feb 4, 2025 · 5 comments

Comments

@precrati
Copy link

precrati commented Feb 4, 2025

Required Reading

  • Confirmed

Plugin Version

4.16.6

Mobile operating-system(s)

  • iOS
  • Android

What do you require assistance about?

Hello! Have been connecting this SDK to my application for a few days and got stuck at one point.
I need to send some of the data which is being captured by SDK to my backend in a specific format.
After reading the documentation properly i've found that there is a "locationTemplate" object which can do what i need.
So, my backend is waiting for this json in body request:

{
    "lat": 123,
    "lon": 123
}

Snippet of my location template will be in a code section but to be sure i'll write it here as well:
{"lat":<%= latitude %>,"lon":<%= longitude %>}

The thing is - when i open the application everything loads fine except for the data which is being sent to backend is not formatted to location template but sent as raw object (with all the objects that sdk can capture (as far as i understand))
When i debug i can see that backend gets the request but since the format is wrong i get status 400 with my backend error:
{"status":"error","errors":{"lat":["This field is required."],"lon":["This field is required."]}}

[Optional] Plugin Code and/or Config

static const _locationTemplate =
      '{"lat":<%= latitude %>,"lon":<%= longitude %>}';

  Future<Config> _getEnableConfig() async {
    final apiUrl = 'my url';
    final authHeaders = await AuthInterceptor().generateHeaders();
    final baseHeaders = await BaseHeadersInterceptor().generateHeaders();
    final headers = <String, dynamic>{};
    headers.addAll(authHeaders);
    headers.addAll(baseHeaders);

    return Config(
      //Common
      debug: true,
      triggerActivities: 'on_bicycle',
      activityRecognitionInterval: 10000,
      desiredAccuracy: Config.DESIRED_ACCURACY_HIGH,
      distanceFilter: 1.0,
      stopOnTerminate: false,
      startOnBoot: true,
      logLevel: Config.LOG_LEVEL_VERBOSE,
      locationTemplate: _locationTemplate,

      //Android only
      enableHeadless: true,
      foregroundService: true,
      notification: Notification(
        title: 'title',
        text: 'text',
        // color: '#7445F5',
        smallIcon: 'mipmap/ic_launcher',
        largeIcon: 'mipmap/ic_launcher',
        priority: Config.NOTIFICATION_PRIORITY_HIGH,
      ),

      //IOS only
      activityType: Config.ACTIVITY_TYPE_FITNESS,

      /// Backend
      url: apiUrl,
      method: 'POST',
      headers: headers,
    );

[Optional] Relevant log output

@christocracy
Copy link
Member

You likely have previously recorded locations in the plugin's database. Destroy them with .destroyLocations method (or review them with .locations or .count.

The plugin renders the template once then inserts the rendered data into SQLIte.

@precrati
Copy link
Author

precrati commented Feb 5, 2025

Thank you very much for your answer.
I've tried your solution with firstly destroying locations which i could have and after that reloaded the application (tried to print .count and it showed me 0) but unfortunately i still keep getting this exact error while having a breakpoint at:

 BackgroundGeolocation.onHttp((event) {
      print(event);
    });

Can i provide some additional information or maybe more code snippets which could help you identify what is going on? If so, please share - and i will upload it as fast as i can.

@christocracy
Copy link
Member

I suggest you log the received json at your server and find verify you’re receiving the format you’re expecting.

@precrati
Copy link
Author

Thanks, perfect!
The problem was that i've been waiting on backend in this format:

{
    "lat": 123,
    "lon": 123
}

But when i've checked what is being received was in this format:

{
  "location": {
       "lat": 123,
       "lon": 123
    }
}

And one more question if it's possible.
Am i right that from the config (which i've attached above) means that request to backend will be triggered only if SDK identifies your movements as if you are on bicycle?
Because i took a bike for a quick ride with the app installed on my Android phone, with debug mode i've heard that the sdk is being triggered but when i checked in my admin panel i saw that activity.type is always still. (could have it as running using Lockito and shaking my phone intensively).

my template is

static const _locationTemplate =
      '{"lat": <%= latitude %>, "lon":<%= longitude %>, "is_moving": <%= is_moving %>, "type":"<%= activity.type %>"}';

and what i receive

Image

@christocracy
Copy link
Member

When something unusual occurs, the first thing to do is fetch the plug-in’s log database. See wiki “Debugging” and learn to use method .emailLog.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants