flutter_background_fetch icon indicating copy to clipboard operation
flutter_background_fetch copied to clipboard

[BUG] Background task on boot does not run with app's permissions

Open tomkcook opened this issue 3 years ago • 1 comments

Your Environment

  • Plugin version: 1.1.0
  • Platform: Android
  • OS version: 11
  • Device manufacturer / model: OnePlus 7
  • Flutter info (flutter info, flutter doctor):
Flutter 3.0.1 • channel stable • https://github.com/flutter/flutter.git
Framework • revision fb57da5f94 (4 months ago) • 2022-05-19 15:50:29 -0700
Engine • revision caaafc5604
Tools • Dart 2.17.1 • DevTools 2.12.2
  • Plugin config - no idea what this is

To Reproduce Steps to reproduce the behavior:

Code:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  BackgroundFetch.registerHeadlessTask(headlessTask);
  BackgroundFetch.configure(BackgroundFetchConfig(
    minimumFetchInterval: 15,
    stopOnTerminate: false,
    enableHeadless: true,
    startOnBoot: true,
    requiresBatteryNotLow: false,
    requiresStorageNotLow: false,
    requiresCharging: false,
    requiresDeviceIdle: false,
    requiredNetworkType: NetworkType.NONE
  ), (String taskId) {
    BackgroundFetch.finish(taskId);
  });
  BackgroundFetch.scheduleTask(TaskConfig(
    delay: 0,
    periodic: false,
    startOnBoot: true,
    taskId: "app_on_boot",
    enableHeadless: true
  ));
  runApp(await MyApp.create());
}

void headlessTask(HeadlessTask task) async {
  if (Platform.isAndroid) {
    log("Checking permissions");
    status = await Permission.locationAlways.status;
    locationAlwaysGranted = status.isGranted;
    if (locationAlwaysGranted) {
      log("We have location permissions");
    }
  }
}

The MyApp class needs to request the locationAlways permission, with appropriate user prompting (and requesting the location status first of course).

  1. Install this app on a phone and start it.
  2. Accept the permissions.
  3. Then kill the app and restart it. Both the Checking permissions and We have location permissions log messages appear.
  4. Now restart the phone. Only the Checking permissions message appears, not the We have location permissions message.

Additional context I don't know if this is me doing something wrong, expected behaviour or a defect. If it's expected behaviour, it seems to limit the utility of the startOnBoot option a great deal, as it's not possible to use any of the apps permissions when starting from boot.

I hope I'll get 3/4 hour later today to post a full example in a github repo demonstrating the problem - the above is cut-down from a larger app.

tomkcook avatar Oct 01 '22 08:10 tomkcook

Perhaps you need to take this issue up with this "Permissions" plugin?

christocracy avatar Oct 03 '22 14:10 christocracy

Apologies it's taken a long time to get back to this. The exception that happens when trying to check permissions from a background_fetch task is:

E/flutter ( 6464): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: PlatformException(PermissionHandler.PermissionManager, Unable to detect current Android Activity., null, null)
E/flutter ( 6464): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
E/flutter ( 6464): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:167:18)
E/flutter ( 6464): <asynchronous suspension>
E/flutter ( 6464): #2      MethodChannelPermissionHandler.requestPermissions (package:permission_handler_platform_interface/src/method_channel/method_channel_permission_handler.dart:71:9)
E/flutter ( 6464): <asynchronous suspension>
E/flutter ( 6464): #3      GFZModel.updatePermissions (package:gfz_app/model.dart:349:24)
E/flutter ( 6464): <asynchronous suspension>

It appears that the Permissions plugin needs to be able to determine a current Activity to get the request permisisons and this is not possible. I'll raise this with the maintainers of Permissions but if you have any ideas on how to make this work I'd be grateful.

tomkcook avatar Dec 05 '22 15:12 tomkcook

I've got no experience at all with this "Permissions Plugin". I've never used it.

christocracy avatar Dec 05 '22 17:12 christocracy