Reputation: 397
I made an audio application that shows a foreground notification in the notification tray with play/pause controls. It was working fine but after Android S started to get the "Targeting S+ (version 31 and above) requires..." error. Here is the little piece of code responsible for crash.
val notificationBuilder = NotificationCompat.Builder(context)
notificationBuilder.setContentTitle(description.title)
notificationBuilder.setContentText(description.subtitle)
notificationBuilder.setSubText(description.description)
notificationBuilder.setContentIntent(pendingIntent)
notificationBuilder.setAutoCancel(true)
notificationBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
notificationBuilder.setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_STOP))
MediaButtonReceiver.buildMediaButtonPendingIntent() is giving the crash for not giving FLAG_IMMUTABLE or FLAG_MUTABLE for the pending intent. But that is within android class itself and out of control.
Here is the crash report:
Caused by: java.lang.IllegalArgumentException: com.islambook: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:632)
at androidx.media.session.MediaButtonReceiver.buildMediaButtonPendingIntent(MediaButtonReceiver.java:274)
at androidx.media.session.MediaButtonReceiver.buildMediaButtonPendingIntent(MediaButtonReceiver.java:234)
at com.islambook.Audio.MediaStyleHelper.from(MediaStyleHelper.kt:42)
Upvotes: 2
Views: 806
Reputation: 397
Found the solution. I was using old media classes. I had to add this to app > build.gradle
implementation("androidx.media:media:1.6.0")
Upvotes: 3