ryanheise / audio_service

Flutter plugin to play audio in the background while the screen is off.
806 stars 480 forks source link

chore: add checks to allow SDK 34(android 14+) to start foreground se… #1088

Closed JoelFickson closed 2 months ago

JoelFickson commented 2 months ago

chore: add support for android 14 and above to start foreground services.

If there's an open issue your PR is fixing, please list it here.

Pre-launch Checklist

ryanheise commented 2 months ago

Thank you, but isn't this unnecessary since it is declared in the manifest?

JoelFickson commented 2 months ago

Thank you, but isn't this unnecessary since it is declared in the manifest? Actually, there is a need for that since Android 14:

Check https://developer.android.com/about/versions/14/changes/fgs-types-required#permission-for-fgs-type

I am was testing it on Android 14 and the media notification doesn't show up on my Google Pixel 7 Pro and even emulator on Pixel 8 Pro.

ryanheise commented 2 months ago

My question was specifically about whether doing this at runtime is necessary "given" that this is already specified in the manifest. In other words, are you wanting to change this because you believe the manifest approach is wrong and/or doesn't work, or are you wanting to change this because you think it is better practice to do it at runtime rather than in the manifest?

JoelFickson commented 2 months ago

@ryanheise its because the current approach doesnt work. As I referenced the link to the docs, there needs to be this runtime check. Seems the current approach works for Android <=13 but not >13. We need both approaches.

ryanheise commented 2 months ago

That would seems surprising, since even the page you linked to says this:

If the foreground service type is not specified in the call, the type defaults to the values defined in the manifest. If you didn't specify the service type in the manifest, the system throws MissingForegroundServiceTypeException.

JoelFickson commented 2 months ago

There is also this part.

Screenshot 2024-08-29 at 4 30 20 PM

I am not only just trying to propose these changes, I'm proposing because I do run into the following error:

android.app.MissingForegroundServiceTypeException: Starting FGS without a type callerApp=ProcessRecord{aa0db08 4727:com.xxx.app/u0a202} targetSDK=34 W/System.err( 4727): at android.app.MissingForegroundServiceTypeException$1.createFromParcel(MissingForegroundServiceTypeException.java:53) W/System.err( 4727): at android.app.MissingForegroundServiceTypeException$1.createFromParcel(MissingForegroundServiceTypeException.java:49)

ryanheise commented 2 months ago

Then let's put aside that is a better practice because that is really a lower priority that pointing out that the current approach doesn't work. I am saying I highly doubt that the current approach doesn't work because the documentation says it does work, and people are using it successfully in Android 14.

If you're saying you've already put it in the manifest and yet it doesn't work, can you perhaps share your manifest so that I can see it?

JoelFickson commented 2 months ago

My bad! We can close the issue. This seems to work.


            android:name="com.ryanheise.audioservice.AudioService"
            android:exported="true"
            android:foregroundServiceType="mediaPlayback">
            <intent-filter>
                <action android:name="android.media.browse.MediaBrowserService" />
            </intent-filter>
        </service>```