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

Issue in android 14 (API 34) #239

Closed owl-madness closed 1 month ago

owl-madness commented 1 month ago

We were trying to enable screen share feature in a video conference. It need to run a service in the background. So using this package enabled the feature, but when changed the 'targetSdkVersion' from 33 to 34, because playstore sent an warning mail to change the targetSdkVersion otherwise app will be removed from the playstore. This change encountered with an issue. Error is given below,

sample code is

//service initiate

FlutterForegroundTask.init(
      androidNotificationOptions: AndroidNotificationOptions(
        channelId: 'notification_channel_id',
        channelName: 'Foreground Notification',
        channelDescription:
            'This notification appears when the foreground service is running.',
        channelImportance: NotificationChannelImportance.HIGH,
        priority: NotificationPriority.HIGH,
        iconData: const NotificationIconData(
          resType: ResourceType.mipmap,
          resPrefix: ResourcePrefix.ic,
          name: 'launcher',
        ),
      ),
      iosNotificationOptions: const IOSNotificationOptions(
        showNotification: true,
        playSound: false,
      ),
      foregroundTaskOptions: const ForegroundTaskOptions(
        interval: 5000,
        isOnceEvent: false,
        autoRunOnBoot: true,
        allowWakeLock: true,
        allowWifiLock: true,
      ),
    );

//starting service

await FlutterForegroundTask.startService(
          notificationText: "Screensharing active",
          notificationTitle: "Screensharing");
Dev-hwang commented 1 month ago

This plugin already supports API 34 version. Please tell me what error occurred.

owl-madness commented 1 month ago

This plugin already supports API 34 version. Please tell me what error occurred.

D/AndroidRuntime(25242): Shutting down VM
E/AndroidRuntime(25242): FATAL EXCEPTION: main
E/AndroidRuntime(25242): Process: com.xxxxxx.xxxxxx, PID: 25242
E/AndroidRuntime(25242): java.lang.RuntimeException: Unable to create service com.pravera.flutter_foreground_task.service.ForegroundService: java.lang.SecurityException: Starting FGS with type mediaProjection callerApp=ProcessRecord{ce3060e 25242:com.xxxxxx.xxxxxx/u0a208} targetSDK=34 requires permissions: all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION] any of the permissions allOf=false [android.permission.CAPTURE_VIDEO_OUTPUT, android:project_media] 
E/AndroidRuntime(25242):        at android.app.ActivityThread.handleCreateService(ActivityThread.java:4664)
E/AndroidRuntime(25242):        at android.app.ActivityThread.-$$Nest$mhandleCreateService(Unknown Source:0)
E/AndroidRuntime(25242):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2264)
E/AndroidRuntime(25242):        at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime(25242):        at android.os.Looper.loopOnce(Looper.java:205)
E/AndroidRuntime(25242):        at android.os.Looper.loop(Looper.java:294)
E/AndroidRuntime(25242):        at android.app.ActivityThread.main(ActivityThread.java:8177)
E/AndroidRuntime(25242):        at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(25242):        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
E/AndroidRuntime(25242):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
E/AndroidRuntime(25242): Caused by: java.lang.SecurityException: Starting FGS with type mediaProjection callerApp=ProcessRecord{ce3060e 25242:com.xxxxxx.xxxxxx/u0a208} targetSDK=34 requires permissions: all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION] any of the permissions allOf=false [android.permission.CAPTURE_VIDEO_OUTPUT, android:project_media] 
E/AndroidRuntime(25242):        at android.os.Parcel.createExceptionOrNull(Parcel.java:3057)
E/AndroidRuntime(25242):        at android.os.Parcel.createException(Parcel.java:3041)
E/AndroidRuntime(25242):        at android.os.Parcel.readException(Parcel.java:3024)
E/AndroidRuntime(25242):        at android.os.Parcel.readException(Parcel.java:2966)
E/AndroidRuntime(25242):        at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:6761)
E/AndroidRuntime(25242):        at android.app.Service.startForeground(Service.java:862)
E/AndroidRuntime(25242):        at com.pravera.flutter_foreground_task.service.ForegroundService.startForegroundService(ForegroundService.kt:224)
E/AndroidRuntime(25242):        at com.pravera.flutter_foreground_task.service.ForegroundService.onCreate(ForegroundService.kt:84)
E/AndroidRuntime(25242):        at android.app.ActivityThread.handleCreateService(ActivityThread.java:4651)
E/AndroidRuntime(25242):        ... 9 more
E/AndroidRuntime(25242): Caused by: android.os.RemoteException: Remote stack trace:
E/AndroidRuntime(25242):        at com.android.server.am.ActiveServices.validateForegroundServiceType(ActiveServices.java:2611)
E/AndroidRuntime(25242):        at com.android.server.am.ActiveServices.setServiceForegroundInnerLocked(ActiveServices.java:2322)
E/AndroidRuntime(25242):        at com.android.server.am.ActiveServices.setServiceForegroundLocked(ActiveServices.java:1679)
E/AndroidRuntime(25242):        at com.android.server.am.ActivityManagerService.setServiceForeground(ActivityManagerService.java:13281)
E/AndroidRuntime(25242):        at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:3385)
E/AndroidRuntime(25242): 
I/Process (25242): Sending signal. PID: 25242 SIG: 9
Lost connection to device.

this is the error in the console.

Dev-hwang commented 1 month ago

https://developer.android.com/about/versions/14/changes/fgs-types-required#media-projection

Check this page

https://developer.android.com/about/versions/14/changes/fgs-types-required#media-projection

foregroundServiceType, permission, and Runtime prerequisites must be met :)

owl-madness commented 1 month ago

permissions added in androidmanifest files are,

<uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_REMOTE_MESSAGING" />

and service

<service
            android:name="com.pravera.flutter_foreground_task.service.ForegroundService"
            android:foregroundServiceType="mediaProjection"
            android:stopWithTask="true" />

error :

E/AndroidRuntime( 7645): java.lang.RuntimeException: Unable to create service com.pravera.flutter_foreground_task.service.ForegroundService: java.lang.SecurityException: Starting FGS with type mediaProjection callerApp=ProcessRecord{bd40d5a 7645:com.nearplatforms.nearvideoconference/u0a190} targetSDK=34 requires permissions: all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION] any of the permissions allOf=false [android.permission.CAPTURE_VIDEO_OUTPUT, android:project_media] 
E/AndroidRuntime( 7645):        at android.app.ActivityThread.handleCreateService(ActivityThread.java:4664)
E/AndroidRuntime( 7645):        at android.app.ActivityThread.-$$Nest$mhandleCreateService(Unknown Source:0)
E/AndroidRuntime( 7645):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2264)
E/AndroidRuntime( 7645):        at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime( 7645):        at android.os.Looper.loopOnce(Looper.java:205)
E/AndroidRuntime( 7645):        at android.os.Looper.loop(Looper.java:294)
E/AndroidRuntime( 7645):        at android.app.ActivityThread.main(ActivityThread.java:8177)
E/AndroidRuntime( 7645):        at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 7645):        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
E/AndroidRuntime( 7645):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
E/AndroidRuntime( 7645): Caused by: java.lang.SecurityException: Starting FGS with type mediaProjection callerApp=ProcessRecord{bd40d5a 7645:com.nearplatforms.nearvideoconference/u0a190} targetSDK=34 requires permissions: all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION] any of the permissions allOf=false [android.permission.CAPTURE_VIDEO_OUTPUT, android:project_media] 
E/AndroidRuntime( 7645):        at android.os.Parcel.createExceptionOrNull(Parcel.java:3057)
E/AndroidRuntime( 7645):        at android.os.Parcel.createException(Parcel.java:3041)
E/AndroidRuntime( 7645):        at android.os.Parcel.readException(Parcel.java:3024)
E/AndroidRuntime( 7645):        at android.os.Parcel.readException(Parcel.java:2966)
E/AndroidRuntime( 7645):        at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:6761)
E/AndroidRuntime( 7645):        at android.app.Service.startForeground(Service.java:862)
E/AndroidRuntime( 7645):        at com.pravera.flutter_foreground_task.service.ForegroundService.startForegroundService(ForegroundService.kt:224)
E/AndroidRuntime( 7645):        at com.pravera.flutter_foreground_task.service.ForegroundService.onCreate(ForegroundService.kt:84)
E/AndroidRuntime( 7645):        at android.app.ActivityThread.handleCreateService(ActivityThread.java:4651)
E/AndroidRuntime( 7645):        ... 9 more
E/AndroidRuntime( 7645): Caused by: android.os.RemoteException: Remote stack trace:
E/AndroidRuntime( 7645):        at com.android.server.am.ActiveServices.validateForegroundServiceType(ActiveServices.java:2611)
E/AndroidRuntime( 7645):        at com.android.server.am.ActiveServices.setServiceForegroundInnerLocked(ActiveServices.java:2322)
E/AndroidRuntime( 7645):        at com.android.server.am.ActiveServices.setServiceForegroundLocked(ActiveServices.java:1679)
E/AndroidRuntime( 7645):        at com.android.server.am.ActivityManagerService.setServiceForeground(ActivityManagerService.java:13281)
E/AndroidRuntime( 7645):        at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:3385)
E/AndroidRuntime( 7645): 
W/videoconference( 7645): Attempt to remove non-JNI local reference
I/Process ( 7645): Sending signal. PID: 7645 SIG: 9
Lost connection to device.
Dev-hwang commented 1 month ago

Have you checked the link I commented?

image

To use mediaProjection, you need FOREGROUND_SERVICE_MEDIA_PROJECTION permission.

And before starting the service you need to call createScreenCaptureIntent() function on Android.

You must implement the code that calls this yourself.

Dev-hwang commented 1 month ago

move issue #226