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

Add support for background location tracking #53

Open
martijn00 opened this issue Jul 31, 2018 · 97 comments
Open

Add support for background location tracking #53

martijn00 opened this issue Jul 31, 2018 · 97 comments
Labels
background-foreground-location P1 High-priority issues at the top of the work list. platform: android Issue is related to the Android platform. platform: ios Issue is related to the iOS platform type: enhancement New feature or request

Comments

@martijn00
Copy link
Member

Also see: flutter/plugins#656

@martijn00 martijn00 added the type: enhancement New feature or request label Jul 31, 2018
@martijn00 martijn00 added this to the 1.5 milestone Jul 31, 2018
@eliashdezr
Copy link

eliashdezr commented Aug 8, 2018

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!

@mvanbeusekom mvanbeusekom modified the milestones: 1.5, 1.6 Aug 14, 2018
@aidandavis
Copy link

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.

@mvanbeusekom mvanbeusekom modified the milestones: 1.6, 2.0 Aug 20, 2018
@aidandavis
Copy link

aidandavis commented Sep 11, 2018

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.

@velegip
Copy link

velegip commented Sep 11, 2018

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

@aidandavis
Copy link

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.

@sestegra
Copy link
Contributor

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.

@mvanbeusekom mvanbeusekom modified the milestones: 2.0, 2.1.0 Sep 13, 2018
@mvanbeusekom mvanbeusekom removed this from the 2.1.0 milestone Oct 12, 2018
@JesseRMeyer
Copy link

JesseRMeyer commented Oct 21, 2018

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.

@dark-chocolate
Copy link

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

@ibrierley
Copy link

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.

@dark-chocolate
Copy link

dark-chocolate commented Dec 6, 2018

@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.

@ibrierley
Copy link

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).

@angel1st
Copy link

@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...

@ashihCT
Copy link

ashihCT commented Feb 1, 2019

Hope you guys can add BG service soon :)

@kocholes
Copy link

kocholes commented Feb 14, 2019

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

@lambasoft
Copy link

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

@kostiaalyo
Copy link

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+

@gregology
Copy link

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

@vishweshsoni

This comment has been minimized.

@nikoskip
Copy link

nikoskip commented May 7, 2019

Looking forward to this!

@PaoloP98
Copy link

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?

@mbilalakmal
Copy link

mbilalakmal commented Mar 14, 2021

@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).

@guigzp
Copy link

guigzp commented Jun 2, 2021

Any updates on this? I just wanted to listen to updates while the app is not killed, like @mbilalakmal said. The location package already does this, but I prefer this package structure and API.

@jesussmile
Copy link

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

Hi @temala I'm trying to do exactly what you have done! Could you please help me out with a reference or code snippet or anything to use background location with firebase ?

@nanjadev
Copy link

nanjadev commented Oct 25, 2021

Not perfect solution but if you want still use the Geolocator plugin in background mode (not terminated):

The flutter-geolocator works fine with background_location plugin.
after run BackgroundLocation.startLocationService() the flutter-geolocator just start working in background mode 😃

Works on iOS and Android.

Example code:

import 'package:geolocator/geolocator.dart';
import 'package:background_location/background_location.dart';

// Start location background service from "background_location".
BackgroundLocation.startLocationService();

// Stream subscription from "flutter-geolocator".
StreamSubscription<ServiceStatus> serviceStatusStream = Geolocator.getServiceStatusStream().listen(
(ServiceStatus status) {
   print(status);
});

@CodeOutput
Copy link

@nanjadev Can you share the full code which package and versions?
thansk you

@nanjadev
Copy link

nanjadev commented Oct 29, 2021

@nanjadev Can you share the full code which package and versions? thansk you

This is exactly what we have in code.
background_location : 0.8.1
geolocator: 7.7.0

@CodeOutput
Copy link

@nanjadev thanks you...

@ICP-Theo
Copy link

ICP-Theo commented Jan 8, 2022

I have a bike app that get location every second. Up to version 7.03 we could get the location when the app was running in the backgound ( background as defined by mbilalakmal, see his comment 14 mar 2021). After upgrading we no longer could get the location when the app was in the backgound and we were forced to downgrade to version 7.03.
I just tested the latest version (version 8.01) but also this version prevents us to get the location with the app in the background.
Perhaps you can see what the difference is coming from version 7.03 and solve the issue.

@ICP-Theo
Copy link

Is it possible that one of the dependencies is causing the background permission problems?
Anyone any suggestions on this problem??

@cto-leaps
Copy link

I have a bike app that get location every second. Up to version 7.03 we could get the location when the app was running in the backgound ( background as defined by mbilalakmal, see his comment 14 mar 2021). After upgrading we no longer could get the location when the app was in the backgound and we were forced to downgrade to version 7.03.
I just tested the latest version (version 8.01) but also this version prevents us to get the location with the app in the background.
Perhaps you can see what the difference is coming from version 7.03 and solve the issue.

@ICP-Theo Do you have example of code of using a Foreground Service and Geolocator (7.0.3)? I've been trying to implement something to no avaiL...

@ICP-Theo
Copy link

ICP-Theo commented Feb 6, 2022

@cto-leaps
My focus was IOS to start with, so I set all the keys in the info.plist:
UIBackgroundModes

fetch
location
bluetooth-central

NSLocationAlwaysAndWhenInUseUsageDescription
We need your location to create a route that you have cycled. Yes even when you put your devices in sleep mode you want to track your route
NSLocationAlwaysUsageDescription
We need your location to create a route that you have cycled. Yes even when you put your devices in sleep mode you want to track your route
NSLocationWhenInUseUsageDescription
We need your location to create a route that you have cycled. Yes even when you put your devices in sleep mode you want to track your route

In this version I did not pay any attention to Android but I had an older version written in C++ with a foreground service to get the location when the app ran in the background and that worked quite well.
When I have solved this issue for IOS I wil look into the foreground service for Android

@jesussmile
Copy link

I have a bike app that get location every second. Up to version 7.03 we could get the location when the app was running in the backgound ( background as defined by mbilalakmal, see his comment 14 mar 2021). After upgrading we no longer could get the location when the app was in the backgound and we were forced to downgrade to version 7.03. I just tested the latest version (version 8.01) but also this version prevents us to get the location with the app in the background. Perhaps you can see what the difference is coming from version 7.03 and solve the issue.

Exactly the same issue, did you find any fix ?

@cto-leaps
Copy link

cto-leaps commented Feb 23, 2022

Exactly the same issue, did you find any fix ?

Context: My fitness app is used for my user to track their position while they perform certain activities they launch themselves. I am not trying to detect or track them when they are not using the app.
I do, however, need to track them when the app is not in the foreground but an activity they have launched is still running.

In the end, I used two separate approaches.

  • On iOS, I simply used GeoLocator and added the "Background updates" permissions in the plist. I ask the user for "While the app is in use" permission. I subscribe to the Stream from the main isolate and I get position updates every x meters whether the app is in foreground or background. It works flawlessly. No need for an "Always" permission as I don't need updates when the app is killed.

  • Different story on Android: when the user launches an activity, I launch a foreground service. It's that foreground service who's in charge of listening to the GeoLocator stream and sends the updates to the main isolate, keeping the app "alive" even if it's in the background.
    There again, I only need the "While in use" permission. I kill the foreground when the activity is stopped by the user.
    I initially tried to spin the foreground service only when the app was going in the background, but the GeoLocator was getting lost with the streams...

I use the latest version of GeoLocator and everything seems to work fine on both platforms. I'm still in test on Android and waiting for the app to be approved. But so far, so good.

I hope this helps, @jesussmile !

@ICP-Theo
Copy link

@jesussmile
Like I said my focus was iOS, so I started my own solution to get the location to see what was going on in the background.
Then xCode mentioned I should increase the target deployment version, so I changed the 'global platform for iOS' in the podfile from 9.0 to 14.1. After that I could get the location even in the background. I went back to Geolocator (global platform for iOS still 14.1) and also now I get the location when the app runs in the background. I know that from version 7.1.0 they changed to a federated plugin and maybe settings for older target versions were not the same (just a guess).
Anyway, this solved it for me for iOS. Will be checking Android soon but priorities are elsewhere at the moment.

I hope this helps someone

@kakashysen
Copy link

I don't know if background location tracking means that the app should stay tracking the user location even if it's killed, in my case I need to track the location when the app goes to background, I had some issues with that like the listener is not executed however I notice one issue in my android configuration I was forcing to use the LocationManager instead of the FusedLocationProviderClient, I just set this configuration forceLocationManager: false, and the background location works

locationSettings = AndroidSettings(
          accuracy: LocationAccuracy.high,
          distanceFilter: 100,
          forceLocationManager: false,
          intervalDuration: const Duration(seconds: 10),
          foregroundNotificationConfig: const ForegroundNotificationConfig(
            notificationText:
                "Example app will continue to receive your location even when you aren't using it",
            notificationTitle: "Running in Background",
            enableWakeLock: true,
          ));

_positionStream =
        Geolocator.getPositionStream(locationSettings: locationSettings)
            .listen((Position position) {
      _lastPosition = _currentPosition;
      _currentPosition = position;
...
}

@JeroenWeener JeroenWeener added P1 High-priority issues at the top of the work list. platform: ios Issue is related to the iOS platform platform: android Issue is related to the Android platform. labels Jul 17, 2023
@spehj
Copy link

spehj commented Sep 12, 2023

Could you let me know if you plan to support background location updates when the app is terminated?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
background-foreground-location P1 High-priority issues at the top of the work list. platform: android Issue is related to the Android platform. platform: ios Issue is related to the iOS platform type: enhancement New feature or request
Projects
None yet
Development

No branches or pull requests