skip to Main Content

today my phone updated to android 12 and my app started to crash when I launch it. It is supposed to send a notification when launched.

val intent = Intent(this, MainActivity::class.java)
        val pendingIntent: PendingIntent? = TaskStackBuilder.create(this).run {
            addNextIntentWithParentStack(intent)
            getPendingIntent(INTENT_REQUEST, PendingIntent.FLAG_IMMUTABLE) //Used to be: FLAG_UPDATE_CURRENT
        }

        //Configurar llamada desde boton de notificacion
        val buttonIntent = Intent(this, LlamadaNotificacion::class.java)
        buttonIntent.putExtra("LLAMADA", "LLAMAR")

        val buttonPendingIntent: PendingIntent =
            PendingIntent.getActivity(this, BUTTON_INTENT_REQUEST, buttonIntent, PendingIntent.FLAG_ONE_SHOT)

        val boton = NotificationCompat.Action.Builder(
            R.drawable.dry,"Presiona para realizar llamada de emergencia", buttonPendingIntent
        ).build()

        val notification = NotificationCompat.Builder(this,channelID).also {
            it.setContentTitle("Daira Mayari Mendez Gutierrez")
            it.setContentText("Presiona para más información")
            it.setSmallIcon(R.mipmap.ic_logo)
            //it.setPriority(NotificationCompat.PRIORITY_HIGH)
            it.setContentIntent(pendingIntent)
            it.setVisibility(VISIBILITY_PUBLIC)
            it.addAction(boton)
            it.setAutoCancel(false)
        }.build()

        val notificationManager = NotificationManagerCompat.from(this)

        notificationManager.notify(notificationId,notification)

I already tried to use mutable and immutable flag, but debbuger says something about an error in "PendingIntent.getActivity" line as follow.

This is an emergency app for my sister who has a suffering, so I would apreciate your help. Thanks.

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.informacion, PID: 11671
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.informacion/com.example.informacion.MainActivity}: java.lang.IllegalArgumentException: com.example.informacion: 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.ActivityThread.performLaunchActivity(ActivityThread.java:4037)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4203)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2440)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:226)
        at android.os.Looper.loop(Looper.java:313)
        at android.app.ActivityThread.main(ActivityThread.java:8641)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1133)
     Caused by: java.lang.IllegalArgumentException: com.example.informacion: 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:382)
        at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:465)
        at android.app.PendingIntent.getActivity(PendingIntent.java:451)
        at android.app.PendingIntent.getActivity(PendingIntent.java:415)
        at com.example.informacion.MainActivity.onCreate(MainActivity.kt:88)
        at android.app.Activity.performCreate(Activity.java:8282)
        at android.app.Activity.performCreate(Activity.java:8262)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4011)
            ... 12 more

2

Answers


  1. You do this:

        val buttonPendingIntent: PendingIntent =
            PendingIntent.getActivity(this, BUTTON_INTENT_REQUEST, buttonIntent, PendingIntent.FLAG_ONE_SHOT)
    

    You have to add FLAG_IMMUTABLE or FLAG_MUTABLE to this one as well.

    Login or Signup to reply.
  2. what you have to do is:
    put in your build.gradle

    def work_version = "2.7.1"
        implementation "androidx.work:work-runtime:$work_version"
        // optional - Test helpers
        androidTestImplementation "androidx.work:work-testing:$work_version"
    

    then

    go to edit> Find > Find in files and search for PendingIntent and if you are using java code put |PendingIntent.FLAG_IMMUTABLE if Kotlin then or PendingIntent.FLAG_IMMUTABLE

    enter image description here

    Your issue will be 100% solved:
    Cheer!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search