rekabhq / background_locator

A Flutter plugin for updating location in background.
MIT License
288 stars 328 forks source link

Breaking background locator on app permission change #252

Open Wian-TMC opened 3 years ago

Wian-TMC commented 3 years ago

Only tested on Android

  1. While the background locator is active and the app is killed, remove the location permissions for the app - Set to deny (Note how the background locator will then stop and the foreground notification will disappear).
  2. Now set the location permissions for the app back to allow ('Only while using app' or 'all the time').
  3. Opening the app and trying to re-initialise background location does nothing in the callcack and does not set the foreground notification message
Wian-TMC commented 3 years ago

I had code as follows:

final isRunning = await BackgroundLocationHelper.isRunning();

if (!isRunning) {
    BackgroundLocationHelper.startTracking(distanceToFilter: 5000.0);
} else {
    await BackgroundLocationHelper.unsubLocationService();
    BackgroundLocationHelper.startTracking(distanceToFilter: 5000.0);
 }

It seems the isRunning() function will return true when changing app permissions on the fly even if the locator exited due to permission changes, so a force to unsub is necessary to get the foreground notification back and the callback to be called again.

So a fix is just always running

await BackgroundLocationHelper.unsubLocationService();
BackgroundLocationHelper.startTracking(distanceToFilter: 5000.0);

irrespective if the Locator is already running or not

Wian-TMC commented 3 years ago

I do not know if this is the intended functionality, so I am re-opening it

Wian-TMC commented 3 years ago

Issue still stands after 1.6.3.

  1. Allow background locator to initialise
  2. Close the app
  3. Change permissions to 'Deny', and then back to 'Always allow'. This kills the background locator service.
  4. Trying to initialise the locator again with registerLocationUpdate() will not run unless you run unRegisterLocationUpdate(). This is because the locator still thinks it is running in the background -> Confirmed with isServiceRunning(), which always returns true after the above procedure.