DeKekem
DeKekem

Reputation: 1743

ForegroundNotificationConfig not showing notification in flutter

To keep tracking user location in my flutter app, I am using the geolocator package as follows:

LocationSettings getLocationSettings() {
 LocationSettings locationSettings;
 if (defaultTargetPlatform == TargetPlatform.android) {
   locationSettings = AndroidSettings(
    //(Optional) Set foreground notification config to keep the app alive
    //when going to the background
    foregroundNotificationConfig: const ForegroundNotificationConfig(
  notificationText:
      "Example app will continue to receive your location even when you aren't using it",
  notificationTitle: "Running in Background",
));
  } else if (defaultTargetPlatform == TargetPlatform.iOS) {
    locationSettings = AppleSettings(
    accuracy: LocationAccuracy.best,
    activityType: ActivityType.automotiveNavigation,
    pauseLocationUpdatesAutomatically: true,
  // Only set to true if our app will be started up in the background.
  showBackgroundLocationIndicator: true,
);
} else {
  locationSettings = const LocationSettings(
    accuracy: LocationAccuracy.best,
  );
}

final positionStream = _geolocatorPlatform.getPositionStream(
      locationSettings: locationSettings);

as described in geolocator documentation. Everything is working fine, I get the location updates even when the app is in the background. My question is to know whether I should see any notification, because I don't see any.

Upvotes: 3

Views: 326

Answers (2)

Reza Aslejeddian
Reza Aslejeddian

Reputation: 371

I am not 100% sure, but it looks like a new configuration was required to be added on Android 14 for me. So, I added the new declarations in the app's manifest with the help of this doc and this issue.

Upvotes: 0

pmatatias
pmatatias

Reputation: 4434

you have to Stream the location.

StreamSubscription<Position> positionStream = Geolocator.getPositionStream(locationSettings: locationSettings).listen(
    (Position? position) {
        print(position == null ? 'Unknown' : '${position.latitude.toString()}, ${position.longitude.toString()}');
    });

this will keep show the notification and as long as the notification is displayed, your app will successfully get the GPS data.

Upvotes: 0

Related Questions