Flutter-geolocator: Add support for background location tracking

Created on 31 Jul 2018  ·  82Comments  ·  Source: Baseflow/flutter-geolocator

Most helpful comment

I was also facing this issue and I developed my own plugin for background location updates, here it is, flutter_background_location ,

All 82 comments

This is one of the main reasons I haven't switch one of my apps to Flutter, background location tracking is a must. Thanks for keeping this on track!

I'm also looking into this. Wanting to build an app in a corporate setting with a background GPS tracking requirement. flutter/plugins#656 (mentioned above) isn't on Pub yet and only supports iOS.
I'll subscribe to this thread for any updates.

I ended up writing my own implementation (on Android, at least), based off this Google sample that runs when the app closes.

I'm pushing the location data to firebase, so I didn't need to send anything back up the method channel. I just upload straight to Firebase using native code when a location result arrives. This means I can't generalise it to help this plugin out, but that sample repo might be useful.

I found a solution to generalize your @aidandavis method.
In this packege all locations stored in sqlite.
If your foreground service can accept store provider sqlite/firebase/... .
With MethodChannel we can get the db file location, and the flutter app can read it.

Update: The Google sample not prevent Doze Mode. If the device uses doze the network and the gps can be killed with your service too.

Update 2: This stackoverflow thread help against chinese roms battery saving things, and adds app to protected list.
This against Doze

That's definitely a way to do it. The problem still remaining is doing something with the data while the app is in background or the sytem chooses to close it.

With regards to Doze, it should only apply when the phone is stationary (like on a table). There's no point tracking location updates at that point anyway - the last location sent prior to Doze would still be accurate.

On top of this, Dart code could be embedded as the same way implemented in alarm_manager_android. The Android callback calls Dart code without the app running.

I see that this feature was removed from the 2.1.0 milestone. Has it been pushed to the back burner? My project also would greatly benefit from this.

Guys, any update on this? This feature can add wings to this plugin. It is must needed.

It's maybe also worth noting that some devices can't get gps locations with the screen off. I've been stuck on this one for a while, and finally found it's down to the devices chipset. So it may be worth double checking that as well, especially if it's a low battery device like a watch.

@ibrierley I am not sure about the wearables but all the devices (Mobile/Tablets) running API >= 16 are capable of getting location updates even when the screen is off. I tested that myself too.

Yes, I've mainly been testing on a Huawei watch 2, and read a thread where there was a post with I think a Huawei engineer (may be wrong), saying it was set in the chipset, so they couldn't fix it (I'll post here if I can find the thread, but haven't seen it on a quick search).

I've ended up turning the screen black, but I hate that solution :) (but it works, just not quite sure if it saves that much battery, so may end up forcing the screen on with something displaying).

@martijn00 I wonder if Geofence plugin won't give a boost on this feature. I believe background execution would be a crucial one and many of us are expecting it. Let me know what do you think...

Hope you guys can add BG service soon :)

Hi!
What is the current status of this issue? is someone working on it? or has been postponed?

Hey guys what are your thoughts on https://pub.dartlang.org/packages/flutter_background_geolocation
Doesn't this plugin provide what is asked here?

Hey guys what are your thoughts on https://pub.dartlang.org/packages/flutter_background_geolocation
Doesn't this plugin provide what is asked here?

It does, but it costs $300+

@christocracy, I think your package is worth every cent however I'm just too poor atm :) Watching this space

hi there is it possible to track live location @christocracy free?

Looking forward to this!

Do you think it will be possible to start work on in it in this year?

Do you think it will be possible to start work on in it in this year?

Anyone can start working immediately. Contributions are welcome.

Maintainers of this project does quite good job on accepting incoming changes from community.

Ok, gimmie resources, and place in code where to start and I try to do my best :D I could spend few weeks to solve it but firstly I need to get in to android deeper :( , I'm just curios if someone is working on it ;)

I appreciate your enthusiasm :1st_place_medal:

gimmie resources, and place in code where to start

I believe that you will have to touch and understand this whole plugin.


However... Few high level design problems should considered before you start coding.

  1. On android newer Android (API 26) only way to get high precision location updates is using Foreground service. Latest (Android Q) also will have additional permission (for getting location in background). So need to address 3 different behavior to have compatibility for: android pre 26, android 26 and android Q
  2. To use Foreground service you need to show system notification. Probably every application will want to have customized notification.
  3. Compatibility with iOS. Geolocator API should hide differences between platform as much as possible.
  4. Foreground Service can be stopped & restarted same as Activity. Need logic to handle this. Flutter doesn't have easy way to do this. Only example I know is alarm_manager plugin. But it buggy as well and doesn't handle all cases. I don't know any open source Flutter library which does this correctly.

For our case we need to track more data for long periods of time (location, bluetooth, sensors). So making every plugin handle long running logic separately is not suitable for use because every plugin would have separate Foreground service and user would see three different Notifications.

I hope google would add this feature in flutter out of the box :p

Hello, is this issue closed? I need the background geolocation too for android and ios for flutter! Which is the best lib for this?

Just to add my +1 to this feature request. For some applications this is a showstopper.
Would be amazing to have it.

+1 Would be so helpful for so many applications

This is highly needed in my project... I was impressed by the one I found on pub, but 300USD?

I was also facing this issue and I developed my own plugin for background location updates, here it is, flutter_background_location ,

+1 Also looking forward to this.

+1 This is very much needed!

  • This feature is needed. Please add this!
  • 1

@milesgranger @Zeissus @shishirraven and others

If you have this issue, instead of writing +1 in comment, give a thumbs up to the issue, it makes more sense.

@AlexCampos95 Why don't you ask me at my repo?

Any solution yet?

any solution ?

any?

The only thing that really works for me is workmanager + geolocator. If battery saving will be disabled for your app, this will work constantly even after phone restart.

Hi, Yegor Vialov

This seems like a good idea. Do you have a sample project that implement
both for background location monitoring? If you do can you please share.

Thanks

On Fri, Dec 13, 2019 at 1:52 AM Yegor Vialov notifications@github.com
wrote:

The only thing that really works for me is workmanager
https://pub.dev/packages/workmanager + geolocator
https://pub.dev/packages/geolocator. If battery saving will be disabled
for your app, this will work constantly even after phone restart.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/Baseflow/flutter-geolocator/issues/53?email_source=notifications&email_token=AFKOFXJ3L4B6SVPCWOUYBLLQYNENLA5CNFSM4FM72VCKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGZKOXY#issuecomment-565356383,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AFKOFXOP3K36MV6LCE3E7HTQYNENLANCNFSM4FM72VCA
.

Workmanager is inconsistent in iOS you dont have any control over when a task would run so it wont work for realtime location updates purposes. On Android the minimum frequency is 15mins so this doesnt work as well.

We really need this to be implemented in flutter out of the box.

I don't have a lot of experience with Android, but it looks like there's a separate call for background location updates compared to foreground ones (which is the one that is currently used in this plugin). Not sure how it would work but I imagine we would want to somehow use the optimal function based on which state the app is in.

Are there any other updates on this? This is a pretty key feature for a location tracking package and would be really nice to have if this is to be the number 1 location package for flutter

@christocracy 's plugin is well worth the money and he responds very, very quickly. If background location is important to your application, it's a nominal cost.

@jamesdixon that library is great. But I have a task manager app where tasks have location notification. Using geolocator + flutter_local_notifications I can trigger push notification when the app is foreground but doesn't work when the app goes background. flutter_background_geolocation is too much for my requirement.

Maybe this lib can help you:
https://github.com/rekab-app/background_locator

Maaaaan you find a gem here its EXACTLY what I need the only problem is that it lack documentation

Maybe this lib can help you:
https://github.com/rekab-app/background_locator

Maaaaan you find a gem here its EXACTLY what I need the only problem is that it lack documentation

I managed to execute their example... It works ok, sometimes it seems that it stops recording the coordinates when I close the main app and it executes the service in background.. But I have to test it again...

Let me know if you need help to execute it :)

It work well for me the only problem is to save on disk in the callback function, the service don't register the plugins native code and thus I can't make native calls using path_provider for example, also if the app is killed the Isolate will send to the ReceivePort from the killed FlutterActivity so nothing is listening. On restart I need to kill the isolate and then recreate it using a new port but I don't think its the best solution lol.
I also found this example that is quite similar to what the library do (have a service that execute dart code with a notification) but they register the plugins entry in the Kotlin file so they are able to store things on the disk
If you know how I can retrieve the LatLng from the callback function it would be awesome🙏

@christocracy would appreciate if you can send a key this way because I have tried all the solutions above and none seem to work consistently (either they terminate after app closes or stop recording). I am currently building a corona tracker at the request of the director of my local hospital. If you can help me out it would be appreciated.

I am currently building a corona tracker at the request of the director of my local hospital. If you can help me out it would be appreciated.

You probably want to fork some existing project on Github instead of starting from scratch

@RomanJos @Ashar2shahid https://corona-trace.github.io/

@christocracy This is exactly what i was trying to build - searching for background-location-plugins for flutter led me here. However the link on this website: https://corona-trace.github.io/
for Slack channel is not working. Do you have another link for slack?

I have gone through the repo - this will save me a lot of work, when i find the $300 plus. Apple is trying to build something similar using bluetooth - they are more concerned about privacy - but i think - location privacy is a small price to pay in these times, building it like this, using google takeout etc is exactly what i had in mind.

@tatiblockchain please come over to my repo to discuss, please.

Why is this such a big deal to have out of the box?

@MazEbeid what do you mean exactly?

I would love to be able to support it, I just don't have the knowledge required to build it. Also didn't find the time to learn how to do it. Please feel free to create a pull-request or maybe you can provide some good resource on how to do it so I can build it (aside from the tutorial from the Flutter site, I have read this many times but having a hard time getting a solid understanding)?

I meant why is the Flutter team not adding a nice and maintainable plugin for background location fetching.

I have used this plugin background_locator which works well but at the moment it doesn't support location updates on iOS when the app is terminated

Joined the club without any hope =(

I'm currently using background_locator package. It's not perfect but works

background_locator is nice... but it has its problems.

I am very interested on finding something more robust (hopefully this here) and I would not mind on having some best practices for storing these values while for example the app is in the background or the screen is off.

I have found that a lot of the options out there are not Isolate-Safe and cause problems such as corrupted data or even just access errors which is not ideal if dealing with a background solution.

@Flavsditz I'm currently planning developing native plugin for bg tracking. I'm probably going to track location by displaying chat bubble (I might be wrong but I think android requires foreground service to keep service alive) on android. But I don't have any idea about tracking on iOS.

I would love to use this plugin, but due to the unavailability of the background location updates, I am forced to choose something else that can provide this feature even if I have to pay $300+. This feature is very important for any application with geolocation.

I would love to use this plugin, but due to the unavailability of the background location updates, I am forced to choose something else that can provide this feature even if I have to pay $300+. This feature is very important for any application with geolocation.

Have you tried this plugin? It has been recently updated and it should support background location.
https://github.com/rekab-app/background_locator

@mvanbeusekom Do you have intentions to include this? It would be incredibly cool. A lot of people need that.

@ndhbr yes I do have the intention to support fetching position updates in the background. However I don't really find the time to do so as of yet. Every time I get started something else (more important) comes up. Now for example iOS 14 is released which introduces a new API for location permissions which needs to be supported for users to be able to use the geolocator on iOS 14.

So if you feel comfortable it would be great if you can make a start, maybe rough edges. The idea I am having is to provide developers an option to register a Dart callback method which will be executed (only) when the App is in the background each time a position update arrives (as described in this article). It would already help a lot if we can make a plan. Break the work that needs to be done into smaller pieces.

@mvanbeusekom
Ah okay, thanks for the update. I can understand that. Thank you for your efforts.

I already know the article almost by heart, but it's still not easy.

I've been trying for a week to get background_locator to run stable (often doesn't work on Android) and they work with isolates. An isolate starts and gets the location in the background. My current procedure is to fill a file byte by byte in the callback and read the file from the last byte in the UI. Unfortunately Android writes the location in the callback only every few minutes and so I lose almost all locations. Under iOS the procedure works well.

Maybe this is also some inspiration.

Is there any STABLE alternative way to handle background location?

And Are you guys decided to add this feature in the future?

Is there any STABLE alternative way to handle background location?

And Are you guys decided to add this feature in the future?

https://github.com/rekab-app/background_locator works well.

@TheMisir Thank you. Are you sure is it working well and is it okay for the production app? There are crash reports too in here https://github.com/rekab-app/background_locator/issues

@TheMisir Thank you. Are you sure is it working well and is it okay for the production app? There are crash reports too in here https://github.com/rekab-app/background_locator/issues

I had no choice. So we're using it in production. Yes it's not works perfectly but it works if configured correctly. (Sometimes issues on our projects was caused by wrong configuration).

Is there any STABLE alternative way to handle background location?

And Are you guys decided to add this feature in the future?

Yes, because I urgently needed the function in production I bought a license for https://www.transistorsoft.com/shop/products/flutter-background-geolocation. I have been using it for 1 month in production and it works fine so far.

Once I add:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
to my manifest (found them in the background_locator example), I could use:
var userPosition = await Geolocator().getCurrentPosition();
in a background fashion using background_fetch: ^0.6.0.

So it's only every 15 minutes but worked pretty well.
Will try background_locator now

Once I add:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
to my manifest (found them in the background_locator example), I could use:
var userPosition = await Geolocator().getCurrentPosition();
in a background fashion using background_fetch: ^0.6.0.

So it's only every 15 minutes but worked pretty well.
Will try background_locator now

I faced this issue on background_locator https://github.com/rekab-app/background_locator/issues/167

And then I moved to the this package https://github.com/transistorsoft/flutter_background_geolocation this package is great

@Dereumaux-Adrien thank you for the update! Really appreciate it. 15 minutes is good enough for me . Will try it..

@bhanuka96 Indeed, it works flawlessly but as I don't yet know if my project has any commercial value I don't intend to spend money on plugins.
I already value transistorsoft as background_fetch is from them as well.
If the project manages to yield a good result, I guess I'll switch to flutter_background_geolocation later on.

TLDR for people new to this thread

Geolocator doesn't fully support the background geolocation yet.

The solutions are the following

If you can pay, https://pub.dev/packages/flutter_background_geolocation works easily and well.
If you can spend time on it, you should manage to make https://pub.dev/packages/background_locator work (couldn't myself yet).
If you want to begin fast, can't spend money yet and are okay with a background check happening every 15~ minutes : https://pub.dev/packages/background_fetch with https://pub.dev/packages/geolocator with manifest changes written here

You can also use WorkManager as detailed here: https://medium.com/@pierre.sabot/how-to-fetch-user-location-in-background-with-flutter-e3494021bdf5

It still limits to 15 minutes for Android, but it's another approach, simple API.

I used https://pub.dev/packages/background_location to track user location in background and save to firestore

Also I am wondering if you don't really would like a callback to be called when a location arrives in the background (so you could proces the location, or safe it somewhere so it can be processed when the app comes back in the foreground).

Not sure how this is handled at the moment in the location plugin. But that is how I was thinking to go about it for the geolocator.

By the way it is not really respectful referring to other plugins.

You can also use WorkManager as detailed here: https://medium.com/@pierre.sabot/how-to-fetch-user-location-in-background-with-flutter-e3494021bdf5

It still limits to 15 minutes for Android, but it's another approach, simple API.

I tried to get the position periodically every 15 minutes and everything works well.
But by setting a periodic event every hour I cannot get the position. The workmanager works (the notification appear) but I can't get the location.
Have you tried to set a periodic event? If so, how long?

@mvanbeusekom should I open a separate issue for supporting location updates while the app is in the foreground?
By foreground, I mean the application is running but the user has tapped the home button, switched to a different app, or the screen has been turned off. For Android, we need to add a foreground service that displays a persistent notification alerting the user that the app is using their location. For iOS, a green icon is displayed in the status bar (this is allowed under NSLocationWhenInUsage permission).
I think supporting this would be much easier and solve many use cases where actual background location updates is not required. (Background location access means the app is launched if terminated when location updates are fetched and the background service can run independent of the app).

Was this page helpful?
0 / 5 - 0 ratings