Cordova-plugin-background-geolocation: Make http requests from the service

Created on 15 Dec 2015  路  27Comments  路  Source: mauron85/cordova-plugin-background-geolocation

Hi!
If I understand correctly how the plugin works, when the app is in background (or killed by the user), the service stores user locations in a database and when the app is opened again it's possible to send these locations to a remote server.

This is fine for some use cases, but do you think it would be possible to make http requests sending user location from the service?

Thank you

enhancement

Most helpful comment

christocracy, I saw your implementation of this functionality, and perhaps that is exactly what I need. But unfortunately I am from Russia. The country is in crisis, and your implementation is prohibitively expensive for me. I trite not be able to spend a quarter of my monthly salary for the library for a non-profit app for personal use. So I quite like everything, but I can not buy. Thank you)

All 27 comments

It only persists locations when app (main activity) is killed by the system or in debug mode. If your app is in the background it will keep receiving location updates.

I actually thrown out the android implementation for making http posts directly from the plugin.

Do you really need it? My point is that there is only one situation that it makes sense and it's "the killed by the system". But from my experience on recent devices (with lot of ram), my test app was never killed by the system and to reproduce such a conditions I had to kill app from terminal.

I'm developing an application that needs to send user location continuously, and it should keep doing it when the app is closed (swiped from the applications list). As far I know, in that case, it's possible to maintain the service running but the main activity doesn't exist, so it should be the service who makes the http request.
Probably this shouldn't be the default behaviour, but I think it would be nice if it's a configurable option.
By the way, this would be possible only if #9 is solved.

I've made for myself storing data on a server using HttpURLConnection in AsyncTask, but it would be great to see it in your plugin, so... +1!)

@Kujbor go ahead push your PR. ;-)

I did it for my realization. But I'll check your implementation, and if it works, then transferred to your plugin and push commit)

@areina @Kujbor Any suggestions about configuration and message POST format?
I would like to make it as simple as possible.

Example:

backgroundGeoLocation.configure(callbackFn, failureFn, {
  postUrl: 'http://some.url/location',
  postAlways: true // false = post locations only when main activity is dead
});

And format of posted message would be same json as received by callbackFn.

{
  "locationId": 1,
  "time": 1450738145721,
  "latitude": 49.1633779,
  "longitude": 20.2710556,
  "accuracy": 40.5,
  "speed": 0,
  "altitude": 0,
  "bearing": 0,
  "serviceProvider": "ANDROID_DISTANCE_FILTER",
  "debug": true
}

Maybe for first iteration is fine to keep it simple, but I'm thinking that some kind of authentication for those requests will be needed and then we should be able to configure headers or params.
About message POST format, I think it's ok.

Thank you for your work!

What may come in handy this functionality without authorization?)) In my implementation, I have sent a request with cookies, that I have taken from webview. In addition, I use get instead of post, because the server is using csrf-protection, and it is much easier to using get-request than an implementation of mechanism for obtaining ssrf token.

mauron85, when I asked in the task #9 where I can place in your plugin my source code to save data on the server, I meant exactly what I asked) I have implemented (in Java for Android) my class that extends AsyncTask for storing data on the server using cookies. Now I want to understand where I can insert my "new BackgroundRequest().execute(params);" in your plugin so that it is guaranteed to be invoked at a location update in the background, and in the foreground (for the Android). Then I create a pull request and you see how I did it))

If you want to save yourself time/money, I've figured out these problems and many more (eg: Android start-on-boot, integrated geofencing). I'm the original author of cordova-background-geolocation, from which this project is a fork of and continues to borrow its ideas from.

http://shop.transistorsoft.com/pages/cordova-background-geolocation-premium

Why is integrated geofencing important? Why not do what this plugin recommends and use an existing 3rd party geofencing plugin? For the case when the plugin is running in _headless_ mode, where the user has closed the app but you still want to continue tracking (ie: stopOnTerminate: false -- for this case, the geofencing implementation must be integrated within the plugin.

christocracy, I saw your implementation of this functionality, and perhaps that is exactly what I need. But unfortunately I am from Russia. The country is in crisis, and your implementation is prohibitively expensive for me. I trite not be able to spend a quarter of my monthly salary for the library for a non-profit app for personal use. So I quite like everything, but I can not buy. Thank you)

I would like to say "+1" for this feature: sending the position to the server while in background or even when the app is killed by the OS is a must.
BUT this feature is useless if we cannot configure the data we are sending from the service.
can you add an "optional" param that we could fill with whatever we want to our server.
backgroundGeoLocation.configure(callbackFn, failureFn, {
postUrl: 'http://some.url/location',
postAlways: true, // false = post locations only when main activity is dead
optional: anotherJson
});

or do not re invent the wheel... just use the data structure christocray used in his plugin:

    // HTTP / SQLite config
    url: 'http://posttestserver.com/post.php?dir=cordova-background-geolocation',
    method: 'POST',
    batchSync: true,       // <-- [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.
    maxDaysToPersist: 1,    // <-- Maximum days to persist a location in plugin's SQLite database when HTTP fails
    headers: {
        "X-FOO": "bar"
    },
    params: {
        "auth_token": "maybe_your_server_authenticates_via_token_YES?"
    }

@clembo590 I would agree that headers are needed, for example if you're using some kind of authentication on the server (Bearer token header...). But IMHO params are unnecessary. You can do the same thing by creating url with params such as http://yourserver.tld/enpoint?param1=val1&param2=arg2. You can adjust server to read those params easily. It would make sense if params could be set individual per request, but that is not the case.

I have done something similar on the Geofence Plugin https://github.com/ElieSauveterre/cordova-plugin-geofence/blob/72848cb3e3bde3af8e0e6a2a5c4c5dbbf9563c6c/src/android/TransitionReceiver.java

I think that the plugin should send location updates when the app is running and when in background et terminated. Headers would be great.

@areina any success in making http requests when app not running? i have the same requirement where application needs to send user location continuously.

Thanks ! help is appreciated

+1, this would be extremely useful for my use-case

I've been doing HTTP in both iOS & Android for over a year in the original Background Geolocation, of which this project is a fork.

@christocracy - Hi Chris - does your plugin do it from the service, even when app is killed by the system (on both platforms)? Thanks

@leobudima yes.

Actually guys, why not support christocracy by buying his product. This plugin would probably never existed without his hard work.

Thanks @mauron85. Here's a 25 % off coupon for first five. Purchase here


Of course - I think it's great work, obviously a lot of effort has gone into it, has great features, great support and it's well worth buying. As a fellow developer, I obviously wish success to him and good sales!

However, in the economy I live/work in, one sometimes needs to find alternatives - and that's why I hugely appreciate your effort, @mauron85, and want to support it where I can. I'm looking into getting on top of some of the problems we have with tracking in our app, and then I'll try to see if I can help with some of the issues or by submitting PRs for additional features we need and use, that others might benefit from.

Sure. I love PRs. Don't hesitate. :-)

@mauron85 , we appreciate your hard work so far and will hang on till we can have the posting to server when app is on background. When will this be possible ?

Also can you share a sample code on how to extract locations saved in when app is on background, you once shared this code
document.addEventListener('deviceready', onDeviceReady, false); function onDeviceReady () { backgroundGeoLocation.getLocations(function(locations) { [].filter.call(locations, function(location) { if (location.debug === false) { //do ajax post of location backgroundGeoLocation.deleteLocation(location.locationId); }; }); }); }

Am yet not able to fetch the saved locations pls help

Hi guys. Next branch currently has very basic support for http posting for Android. Read more in my comment.

https://github.com/mauron85/cordova-plugin-background-geolocation/issues/59#issuecomment-223319855

Anybody willing to try autosync for iOS?

Install next version 3.0.0-alpha.x

cordova plugin add cordova-plugin-mauron85-background-geolocation@next

DOCS:
https://github.com/mauron85/cordova-plugin-background-geolocation/tree/next-ios

Implemented in version 2.2.0

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shyamal890 picture shyamal890  路  10Comments

steveharbick picture steveharbick  路  5Comments

mahmed-ntd picture mahmed-ntd  路  10Comments

Shakti-Sharma picture Shakti-Sharma  路  3Comments

wrunkel picture wrunkel  路  6Comments