Launch an app from background with Pending Intent Android 13

440 Views Asked by At

I am trying to open the app in the background with the button in via a pending intent notification that launches a broadcast receiver. This is the code I use for launching the app in the broadcast receiver.

     val intent = packageManager.getLaunchIntentForPackage(context.applicationInfo.packageName)
                if (intent != null) {
                    intent.setPackage(null)
                    intent.flags =
                        Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
                    context.startActivity(intent)
                }

This code works very well with Android 11 but not on an Android 13 device.

I debugged and found out that these lines are executed but the app is not launched.

Could someone help me with how to launch the app from the background in Android 13?

1

There are 1 best solutions below

0
Mohamed Siad Z On

The issue is that Android does not allow to launch an activity from a Broadcast Receiver which is called from a Pending Intent via a notification.

To improve app performance and UX, apps that target Android 12 or higher can't start activities from services or broadcast receivers that are used as notification trampolines as mentioned in Notification trampoline restrictions. To overcome the issue, instead of using a Broadcast Receiver in Pending Intent's object, use an Activity and handle the Intent's data in onNewIntent in the respective activity.