0

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.

4
  • 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. 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. Commented Mar 28 at 1:21
  • have you tried this, might be useful pub.dev/packages/background_fetch @GoedertDalmolin 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. Commented Mar 28 at 13:33

1 Answer 1

0

You can check out the package flutter_background_geolocation

Also, don't forget to get the

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>

permission.

Sign up to request clarification or add additional context in comments.

1 Comment

The problem with this package (flutter_background_geolocation) is that it's paid. I need to generate releases, and I'm not in a position to pay $300 at the moment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.