Recently, I came across a situation where I needed to save the user's current device location every hour. This needs to happen while the app is completely closed (not just minimized or running in the background).
So far, I haven't found any solution capable of achieving this with Flutter.
I even tried using the silent push concept from Firebase Messaging to trigger a message periodically, wake up the app, and save the location.
Example:
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
@pragma('vm:entry-point')
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
try {
await getAndSaveLocation();
} catch (e) {
log(e.toString());
}
}
However, no Flutter package was able to fetch the location while the app was closed (both on Android and iOS).
I also tried using the WorkManager package but had no success.
Is there any solution for this scenario?
I tested the following packages to try to fetch the location:
location, geolocator, and background_location.
Recently, I came across a situation where I needed to save the user's current device location every hour. This needs to happen while the app is completely closed (not just minimized or running in the background).
So far, I haven't found any solution capable of achieving this with Flutter.
I even tried using the silent push concept from Firebase Messaging to trigger a message periodically, wake up the app, and save the location.
Example:
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
@pragma('vm:entry-point')
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
try {
await getAndSaveLocation();
} catch (e) {
log(e.toString());
}
}
However, no Flutter package was able to fetch the location while the app was closed (both on Android and iOS).
I also tried using the WorkManager package but had no success.
Is there any solution for this scenario?
I tested the following packages to try to fetch the location:
location, geolocator, and background_location.
- If the app is completely quit, I believe you'll need to wake it with a notification that the user must acknowledge. Or, after a bit of googling, you might at least get updates from properly set geofencing. – Randal Schwartz Commented Mar 27 at 21:34
- @RandalSchwartz Is there any way to collect the location without opening the app and without user interaction? That is my goal at the moment. – GoedertDalmolin Commented Mar 28 at 1:21
- have you tried this, might be useful pub.dev/packages/background_fetch @GoedertDalmolin – Abhijith Commented Mar 28 at 5:41
- @Abhijith I've already tested this package (background_fetch) alongside the geolocator package to obtain location. The issue isn't with generating background tasks. The issue is finding a package that can get the location with the app closed. – GoedertDalmolin Commented Mar 28 at 13:33
1 Answer
Reset to default 0You can check out the package flutter_background_geolocation
Also, don't fet to get the
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
permission.