Dev-hwang / flutter_foreground_task

This plugin is used to implement a foreground service on the Android platform.
https://pub.dev/packages/flutter_foreground_task
MIT License
140 stars 105 forks source link

On Android 14+, allow for the subset of declared permissions to be specified at the time of launching the service #210

Closed fareesh closed 3 months ago

fareesh commented 6 months ago

My use-case is as follows

From the relevant Android docs:

Note: If an app that targets API level 28 or higher attempts to create a foreground service without requesting the FOREGROUND_SERVICE permission, the system throws a SecurityException. Similarly, if an app targets API level 34 or higher and doesn't request the appropriate specific permission, the system throws SecurityException.

and

Inside the service, usually in onStartCommand(), you can request that your service run in the foreground. To do so, call ServiceCompat.startForeground() (available in androidx-core 1.12 and higher). This method takes the following parameters:

  • The service
  • A positive integer that uniquely identifies the notification in the status bar
  • The Notification object itself
  • The foreground service types identifying the work done by the service

These types might be a subset of the types declared in the manifest, depending on the specific use case. Then, if you need to add more service types, you can call startForeground() again.

These changes modify AndroidNotificationOptions to allow the developer to specify the bitmap sum of permissions with which the service will be launched (via ServiceCompat.startForeground as mentioned above). This also adds a class of constants AndroidForegroundServiceType.dart for the user to easily sum up the permissions they need:

e.g.

AndroidNotificationOptions(
   ...
   permissionTypes: AndroidForegroundServiceType.MICROPHONE + AndroidForegroundServiceType.MEDIA_PLAYBACK
)
frederikstonge commented 3 months ago

This is already merged. You can use foregroundServiceType and use bitwise operator : foregroundServiceType: AndroidForegroundServiceType.SPECIAL_USE | AndroidForegroundServiceType.CAMERA