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
149 stars 108 forks source link

Crash with Android 14 if type is not defined in Android manifest #192

Closed GaelleJoubert closed 10 months ago

GaelleJoubert commented 10 months ago

I have this issue on my Pixel 6a with Android 14. Whern trying to launch the foreground task, the app crash with the fllowing message :

java.lang.RuntimeException: Unable to create service com.pravera.flutter_foreground_task.service.ForegroundService: android.app.MissingForegroundServiceTypeException: Starting FGS without a type  callerApp=ProcessRecord{907349 32028:fr.kip.app/u0a481} targetSDK=34

I have found the solution, I'll give it here and open a PR to add this info in the documentation.

As it is mentionned here in the Android Guidelines, in Android 14, to start a FG service, you need to specify its type.

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

So you need to add in your manifest the following code :

<manifest ...>
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
    <application ...>
      <service
          android:name="com.pravera.flutter_foreground_task.service.ForegroundService"
          android:foregroundServiceType="mediaPlayback"
          android:exported="false">
      </service>
    </application>
</manifest>

Change the type with your type (all types are listed in the link above). Beware, certzin type have some requirements to be met to work.

erisanolasheni commented 10 months ago

I have the same issue.