Baseflow / flutter-geolocator

Android and iOS Geolocation plugin for Flutter
https://baseflow.com/
MIT License
1.23k stars 642 forks source link

[Foreground service] Notifications are visible on older devices, but not on newer devices. #1238

Open SooChang-Lee opened 1 year ago

SooChang-Lee commented 1 year ago

I'm trying to use Geolocator.getPositionStream() in the background to receive the position continuously for a long time even when the app is closed.

The problem is that notifications are visible on older devices, but not on newer devices.

It seems to be related to the answer in the url below. (Is my guess correct?)

https://stackoverflow.com/a/50634187

It's because of Android O bg services restrictions. So now you need to call startForeground() only for services that were started with startForegroundService() and call it in first 5 seconds after service has been started. Here is the guide - https://developer.android.com/about/versions/oreo/background#services Like this:

//Start service:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(new Intent(this, YourService.class));
} else {
startService(new Intent(this, YourService.class));
}

https://developer.android.com/about/versions/oreo/background#services

Prior to Android 8.0, the usual way to create a foreground service was to create a background service, then promote that service to the foreground. With Android 8.0, there is a complication; the system doesn't allow a background app to create a background service. For this reason, Android 8.0 introduces the new method startForegroundService() to start a new service in the foreground. After the system has created the service, the app has five seconds to call the service's startForeground() method to show the new service's user-visible notification. If the app does not call startForeground() within the time limit, the system stops the service and declares the app to be ANR.

If the notification doesn't appear means the service isn't running as a ForegroundService normally, I'm afraid the service is being killed by the system.

ibelz commented 1 year ago

I have added the notification permission in AndroidManifest.xml <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> and have requested the permission with the "permission_handler" package.

Now the Notification Icon ist back and it seems that the tracking is working again correctly

Myzel394 commented 1 year ago

@ibelz I'm facing the same issue. I added the permission and tried requesting it, just like you did. However, there's no dialog shown for me and on the app settings, it says:

This app does not send notifications

Do you know how to fix this?

gabimem commented 1 year ago

I had the same problem, when starting the foreground service, the notification channel was configured but it was disabled by default, it only worked when manually activating from the application's notification settings.

As I understand it, in the source code, the channel is created with the importance "NONE", therefore it is not displayed.

What I did to fix it is to configure that same channel manually with the "flutter_local_notifications" plugin before starting the foreground service, so then the notification channel is now automatically activated.

This is a snippet of how it worked for me:

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

...

const AndroidNotificationChannel backgroundLocationChannel = AndroidNotificationChannel(
  'geolocator_channel_01', // id
  'Background Location', // name
  description: 'Background Location Notification Channel', // description
  importance: Importance.min,
);
final FlutterLocalNotificationsPlugin plugin = FlutterLocalNotificationsPlugin();
await plugin
    .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
    ?.createNotificationChannel(backgroundLocationChannel);

In my case I was already using that plugin so I didn't have to add weight to my project.

fechnologies-d commented 1 year ago

Hi. Try specifying the service in your android manifest.

<service android:name="the.package.name" android:exported="true" />

Replace the.package.name with the background service package you are using

CalebPourchot commented 9 months ago

I was struggling with this issue, but i discovered it was simply that setOngoing was false by default in the ForegroundNotificationConfig. Setting it to true kept the foreground service alive even when the app went into the background. Hope that helps someone.

EDIT: sorry, i just realized this may be a totally different issue. Y'all may be talking about an actual notification that the user sees and not just the little "location" icon in the status bar that the system uses to indicate someone is using your location. Sorry for the confusion.