I'm trying to show push notifications in Android 12 but I'm getting this error:
IllegalArgumentException Targeting S+ requires that one of FLAG_INMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
To show notifications I'm doing this:
val contentIntent = NavDeepLinkBuilder(this).apply{
setComponentName(MainActivity::class.java)
setGraph(R.navigation.nav_graph)
setDestination(R.id.dialog_custom)
setArguments(arguments)}.createPendingIntent()
val notification = NotificationCompat.Builder(this, channelId).apply{
setSmallIcon(R.drawable.ic_icon_notification)
setContentTitle(...)
setContentText(...)
setStyle(...)
setContentIntent(contentIntent)
setAutoCancel(true)}.build()
How can I fix this error? I tried to add PendingIntent.FLAG_IMMUTABLE in createPendingIntent() arguments but it's not possible. Any idea?
Edit: I'm using version 2.5.1 of Navigation library so, I should have this changes applied in my version https://developer.android.com/jetpack/androidx/releases/navigation#2.4.0-alpha04
Edit 2:
This is the stacktrace:
Fatal Exception: java.lang.IllegalArgumentException: com.example.myapplication: 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:378)
at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:461)
at android.app.PendingIntent.getActivity(PendingIntent.java:447)
at android.app.PendingIntent.getActivity(PendingIntent.java:411)
at com.google.firebase.messaging.CommonNotificationBuilder.createContentIntent(com.google.firebase:firebase-messaging@@20.3.0:125)
at com.google.firebase.messaging.CommonNotificationBuilder.createNotificationInfo(com.google.firebase:firebase-messaging@@20.3.0:27)
at com.google.firebase.messaging.CommonNotificationBuilder.createNotificationInfo(com.google.firebase:firebase-messaging@@20.3.0:9)
at com.google.firebase.messaging.DisplayNotification.handleNotification(com.google.firebase:firebase-messaging@@20.3.0:27)
at com.google.firebase.messaging.FirebaseMessagingService.dispatchMessage(com.google.firebase:firebase-messaging@@20.3.0:65)
at com.google.firebase.messaging.FirebaseMessagingService.passMessageIntentToSdk(com.google.firebase:firebase-messaging@@20.3.0:44)
at com.google.firebase.messaging.FirebaseMessagingService.handleMessageIntent(com.google.firebase:firebase-messaging@@20.3.0:27)
at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(com.google.firebase:firebase-messaging@@20.3.0:17)
at com.google.firebase.messaging.EnhancedIntentService.lambda$processIntent$0$EnhancedIntentService(com.google.firebase:firebase-messaging@@20.3.0:43)
at com.google.firebase.messaging.EnhancedIntentService$$Lambda$0.run(:6)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at com.google.android.gms.common.util.concurrent.zza.run(com.google.android.gms:play-services-basement@@18.0.0:2)
at java.lang.Thread.run(Thread.java:920)