Android 12 - media notification disappearing

Viewed 420

I've been experiencing some odd behavior on Android 12. Specifically with a MediaBrowserService. The app will play sounds in the background for extended periods of time. I am running into two distinct but unwanted behaviors on Android 12.

On a pixel 5a - the notification appears, but will disappear after an hour or so in the background. The music is still playing fine and can be paused if the app is opened up.

On a Samsung s21 - the notification appears when the sound plays. but if it is stopped, and another started, the notification will not appear again upon starting the service until the phone itself is restarted.

On a s9 running Android 10, and a OnePlus 8 running Android 11, there are no issues.

Creating the notification seems pretty straightforward:


companion object {
    const val ACTION_STOP = "action_stop"
    const val CHANNEL_ID = "app_notification_channel"
    const val NOTIFICATION_CHANNEL_TAG = "Playing Controls"
    const val ONGOING_NOTIFICATION_ID = 2222
}

private fun refreshNotification(stopCalled: Boolean = false) {
        val sounds = playingSounds

        if (sounds.isEmpty() || stopCalled) {
            // if we lost audio focus we will expect to restart momentarily. Don't stop the service
            if (!audioFocusTakenBackground) {
                Timber.e("Stopping Foreground")
                stopForeground(true)
            }
            return
        }
        
        val builder = NotificationCompat.Builder(this, CHANNEL_ID).apply {
            color = this@SoundPlayerService.getColor(R.color.magenta_0FF)
            
            val nowPlaying = getString(R.string.action_now_playing)

            setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            setContentTitle(nowPlaying)
            setContentText(name)
            setSmallIcon(R.drawable.ic_icon_small)
            setContentIntent(relaunchPendingIntent())
            setSilent(true)
            setCategory(NotificationCompat.CATEGORY_SERVICE)
            priority = NotificationCompat.PRIORITY_HIGH

            addAction(NotificationCompat.Action(R.drawable.ic_playbar_stop, getString(R.string.action_stop), stopIntent))

            setStyle(
                androidx.media.app.NotificationCompat.MediaStyle()
                    .setShowActionsInCompactView(0)
                    .setMediaSession(mediaSession.sessionToken)
                    .setShowCancelButton(true)
                    .setCancelButtonIntent(stopIntent)
            )
        }

        // If we lost audio focus in the background, we don't stop the service.
        // Ensure that its not called in the background aka failure android 12
        if (!audioFocusTakenBackground && Application.instance.isAppInForeground) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                startForeground(
                    ONGOING_NOTIFICATION_ID,
                    builder.build(),
                    ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
                )
            } else {
                startForeground(
                    ONGOING_NOTIFICATION_ID,
                    builder.build()
                )
            }
        }
    }

The media session is created earlier:

// Create a MediaSession
mediaSession = MediaSessionCompat(this, this.javaClass.name).apply {
     setSessionActivity(sessionActivityPendingActivity)
     isActive = true
}
sessionToken = mediaSession.sessionToken

The media session is then hooked up to an ExoPlayer instance via a MediaSessionConnector

if (mediaSession != null) {
      MediaSessionConnector(mediaSession).also { mediaSessionConnector ->
           mediaSessionConnector.setPlayer(mCurrentPlayer)
      }
}

Again, this hasn't been an issue until 12.

If anyone has seen this or could point me in a way to try and debug, I'd be extremely grateful.

Thanks!

0 Answers
Related