I'm currently having issues with Flutter background service, the moment the app launches, it crashes within a few seconds. I don't know what I'm doing wrong
AndroidManifest.xml
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION" />
Errors
Failed to start foreground service due to SecurityException - have you forgotten to request a permission? - Starting FGS with type mediaProjection callerApp=ProcessRecord{b4e0284 12331:com.zioninvasion.prayer_buddy/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]
MORE ERROR OUTPUT
D/AndroidRuntime(12331): Shutting down VM E/AndroidRuntime(12331): FATAL EXCEPTION: main E/AndroidRuntime(12331): Process: com.zioninvasion.prayer_buddy, PID: 12331 E/AndroidRuntime(12331): android.app.RemoteServiceException$ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{c93a6db u0 com.zioninvasion.prayer_buddy/id.flutter.flutter_background_service.BackgroundService} E/AndroidRuntime(12331): at android.app.ActivityThread.generateForegroundServiceDidNotStartInTimeException(ActivityThread.java:2104) E/AndroidRuntime(12331): at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:2075) E/AndroidRuntime(12331): at android.app.ActivityThread.-$$Nest$mthrowRemoteServiceException(Unknown Source:0) E/AndroidRuntime(12331): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2369) E/AndroidRuntime(12331): at android.os.Handler.dispatchMessage(Handler.java:106) E/AndroidRuntime(12331): at android.os.Looper.loopOnce(Looper.java:205) E/AndroidRuntime(12331): at android.os.Looper.loop(Looper.java:294) E/AndroidRuntime(12331): at android.app.ActivityThread.main(ActivityThread.java:8177) E/AndroidRuntime(12331): at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime(12331): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552) E/AndroidRuntime(12331): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971) E/AndroidRuntime(12331): Caused by: android.app.StackTrace: Last startServiceCommon() call for this service was made here E/AndroidRuntime(12331): at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1952) E/AndroidRuntime(12331): at android.app.ContextImpl.startForegroundService(ContextImpl.java:1906) E/AndroidRuntime(12331): at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:830) E/AndroidRuntime(12331): at androidx.core.content.ContextCompat$Api26Impl.startForegroundService(ContextCompat.java:1189) E/AndroidRuntime(12331): at androidx.core.content.ContextCompat.startForegroundService(ContextCompat.java:752) E/AndroidRuntime(12331): at id.flutter.flutter_background_service.FlutterBackgroundServicePlugin.start(FlutterBackgroundServicePlugin.java:82) E/AndroidRuntime(12331): at id.flutter.flutter_background_service.FlutterBackgroundServicePlugin.onMethodCall(FlutterBackgroundServicePlugin.java:121) E/AndroidRuntime(12331): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267) E/AndroidRuntime(12331): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295) E/AndroidRuntime(12331): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322) E/AndroidRuntime(12331): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12) E/AndroidRuntime(12331): at android.os.Handler.handleCallback(Handler.java:958) E/AndroidRuntime(12331): at android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime(12331): ... 6 more I/Process (12331): Sending signal. PID: 12331 SIG: 9
Package of Interest
flutter_background_service: ^5.0.5
Foreground Services
Foreground services are useful when you want to keep your app running in the foreground, even when the user switches to another app. This can be necessary for features like music players, GPS tracking, or ongoing notifications. flutter_foreground_service
The flutter_foreground_service package enables you to create foreground services on Android and iOS devices. It allows you to display a persistent notification while your app performs its tasks in the foreground.
To add this package to your project, include it in your pubspec.yaml file:
flutter_foreground_service: ^0.2.0
instead of
flutter_background_service: ^5.0.5