0

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)
dimioLt
  • 59
  • 1
  • 12
  • Hey, either update the nav component (see [here](https://stackoverflow.com/a/69829092/3559908)) or add the flag to the `createPendingIntent()` method (see [here](https://stackoverflow.com/a/72676729/3559908)) – Vall0n Jan 13 '23 at 10:54
  • Does this answer your question? [Missing mutability flags: Android 12 pending intents with NavDeepLinkBuilder](https://stackoverflow.com/questions/69829026/missing-mutability-flags-android-12-pending-intents-with-navdeeplinkbuilder) – Vall0n Jan 13 '23 at 10:55
  • @Vall0n I'm using a higher version, I'm using 2.5.1 and still getting the same error. – dimioLt Jan 13 '23 at 11:48
  • Can it be that you are using workManager? – Pako1 Jan 14 '23 at 15:33
  • @Pako1 No, I'm not. – dimioLt Jan 16 '23 at 06:36
  • Hey @dimioLt sry the late response. Can you post the complete stacktrace? – Vall0n Jan 18 '23 at 14:07
  • @Vall0n I updated my answer with it – dimioLt Jan 18 '23 at 15:40
  • Hey, again sry for the late response. You most likely did that already but I ask you anyway. Did you `sync with Gradle Files`, clean and rebuild project after you update the navigation component to `2.5.1`? – Vall0n Jan 23 '23 at 10:00
  • Hey, you might also want to check if you use at least `androidx.work:work-runtime:2.7.0`. Also in the transitive dependencies. Hope that helps! – Vall0n Jan 25 '23 at 08:19

0 Answers0