Skip to content

How to fetch and save the device location periodically while the Flutter app is closed #1024

@GoedertDalmolin

Description

@GoedertDalmolin

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions