uamp icon indicating copy to clipboard operation
uamp copied to clipboard

UAMP crashes with ForegroundServiceStartNotAllowedException when targeting API 31/32

Open DiegoPinedoEscribano opened this issue 2 years ago • 7 comments

The UAMP app crashes when trying to resume the playback session using the Google Assistant if the target and compile sdk is 31 or above

Steps to reproduce it:

  • Checkout the latest version of uamp and update the target and compile sdk to API 31 or 32
  • Fix the compilation issues (adding flags to the manifest and some other classes)
  • Open UAMP app
  • Start playing something
  • Pause it
  • Background the app
  • Open the Google Assistant and say: resume
  • The app crashes:
    android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service com.example.android.uamp.next/com.example.android.uamp.media.MusicService
        at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:54)
        at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:50)
        at android.os.Parcel.readParcelable(Parcel.java:3333)
        at android.os.Parcel.createExceptionOrNull(Parcel.java:2420)
        at android.os.Parcel.createException(Parcel.java:2409)
        at android.os.Parcel.readException(Parcel.java:2392)
        at android.os.Parcel.readException(Parcel.java:2334)
        at android.app.IActivityManager$Stub$Proxy.startService(IActivityManager.java:6028)
        at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1855)
        at android.app.ContextImpl.startForegroundService(ContextImpl.java:1831)
        at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:779)
        at androidx.core.content.ContextCompat$Api26Impl.startForegroundService(ContextCompat.java:933)
        at androidx.core.content.ContextCompat.startForegroundService(ContextCompat.java:701)
        at com.example.android.uamp.media.MusicService$PlayerNotificationListener.onNotificationPosted(MusicService.kt:577)
        at com.google.android.exoplayer2.ui.PlayerNotificationManager.startOrUpdateNotification(PlayerNotificationManager.java:1164)
        at com.google.android.exoplayer2.ui.PlayerNotificationManager.handleMessage(PlayerNotificationManager.java:1409)
        at com.google.android.exoplayer2.ui.PlayerNotificationManager.lambda$ZG0s-7uOzb-YtyUlp8ahx3j4wBg(Unknown Source:0)
        at com.google.android.exoplayer2.ui.-$$Lambda$PlayerNotificationManager$ZG0s-7uOzb-YtyUlp8ahx3j4wBg.handleMessage(Unknown Source:2)

Does the UAMP project still needs to be updated to comply with the latest restrictions around Foreground Services introduced in API 31 ? If this is the case, do you have any idea on when this would happen?

Is it possible that the problem is within the Google Assistant app?

DiegoPinedoEscribano avatar May 31 '22 16:05 DiegoPinedoEscribano

It's a shame that this wasn't addressed one year ago when Android 12 was introduced. Is Google really unaware of this or are they just ignoring the consequences their bad API decisions have on apps? I really want to know how to solve this problem. I mean, they force developers to target the newest API levels and don't provide help on how to address such problems.

And btw, I think this doesn't relate to the Google Assistant because I have seen such crashes on devices where Google services are not available at all. I don't know how to reproduce it locally, but it's a fact that this crash happens on production apps.

stezo avatar Jun 14 '22 12:06 stezo

It's a shame that this wasn't addressed one year ago when Android 12 was introduced. Is Google really unaware of this or are they just ignoring the consequences their bad API decisions have on apps? I really want to know how to solve this problem. I mean, they force developers to target the newest API levels and don't provide help on how to address such problems.

And btw, I think this doesn't relate to the Google Assistant because I have seen such crashes on devices where Google services are not available at all. I don't know how to reproduce it locally, but it's a fact that this crash happens on production apps.

We have opened an issue on their Issue Tracker in case you wanna also follow that one: https://issuetracker.google.com/u/1/issues/235172948

DiegoPinedoEscribano avatar Jun 14 '22 14:06 DiegoPinedoEscribano

Also having this issue on one of our apps. I came here to see how Google solves the problem, only to find ... they haven't.

keithburgoyne avatar Jun 29 '22 19:06 keithburgoyne

Any updates on this? We're also waiting for a solution

ChetanPatelSS avatar Jul 26 '22 08:07 ChetanPatelSS

I think the issue come from this line in Music Service :

        if (!playWhenReady) {
                            // If playback is paused we remove the foreground state which allows the
                            // notification to be dismissed. An alternative would be to provide a
                            // "close" button in the notification which stops playback and clears
                            // the notification.
                            stopForeground(false)
                            isForegroundService = false
                        }

The problem is that notification can't be dismissed since Android 10 with a foreground service, so the team disable the foreground when the media is paused. But as you explained you try to resume with the app is in background and so the service tries to be in foreground again, and the ForegroundServiceStartNotAllowedException happens. A hotfix could be to provide the "close button" and remove this part of the code. Of course, when the notification is dismissed you have to stop/destroy the service. Also the boolean isForegroundService is not really a good solution, Android should provide a listener for the state of the service.

The safest way is to ask the service to be in foreground not inside the service but in an activity/fragment inside your app. If your app is in background you could use AlarmManager to wake up your phone and go to your application.

GauvainSeigneur avatar Jul 26 '22 10:07 GauvainSeigneur

Any Update

mnk98 avatar Jul 27 '22 10:07 mnk98

Hello, Please post an update on this issue. Thank you

guywest avatar Jan 05 '23 07:01 guywest