TL;DR: Answers to How to perform notification-action (click) on lock-screen? don't work anymore cuz now you can't startActivity()
from services or broadcast receivers. Any suggestions on how to achieve this?
DETAILS
I have an app that must open an activity on top of the lock screen. Before targeting Android 12, I could just add an action button to the notification:
val intent = Intent(this, NotificationReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, flags)
val builder = NotificationCompat.Builder(context, channelId)
builder.addAction(drawable, buttonText, pendingIntent)
pendingIntent.getBroadcast()
worked great on the notification action button, because the broadcast will be fired even if the device is unlocked.
The NotificationReceiver
class would then get the broadcast WITHOUT unlocking the phone, and it would open the activity on top of the lock screen from there.
Android 12 introduced Notification Trampoline restrictions, now we cannot call startActivity()
from broadcast receivers or services. I could use pendingIntent.getActivity
instead of pendingIntent.getBroadcast
and launch the Activity directly, but the problem is that getActivity
isn't triggered until AFTER the phone is unlocked (you click on the notification action button > it requests to unlock device > once unlocked, it starts the activity)
Any idea on how to open an activity from a notification without unlocking your phone targeting Android 12+?