Hi everyone. My company's application shows many n...
# android
m
Hi everyone. My company's application shows many notifications to the users. Some notifications have action buttons and we are using BroadcastReceiver to handle when users click the action button. Problem is that we are starting Activity from BroadcastReceiver but Android 10 (API level 29) and higher place restrictions on when apps can start activities when the app is running in the background. So there are some cases users click the button on the notification but nothing happens (activity not start). Does someone have any idea about this? Action button document URL: https://developer.android.com/training/notify-user/build-notification#Actions. Start activities restriction URL: https://developer.android.com/guide/components/activities/background-starts. Btw, I read the document of action button but the start activities restriction was not mentioned there. I think it should be mentioned so I created a document issue. https://issuetracker.google.com/issues/205606716
😶 2
r
I generally make a PendingIntent for a transparent activity in notification clicks and that activity decides which activity to further start instead of using broadcast receiver. It works the same.
m
@Rahul Rawat not notification clicks, problem when click action button on notification.
r
It’s essentially the same thing. You might be using
Copy code
PendingIntent.getBroadcast
to get pending intent of a broadcast receiver what I do usually is use
Copy code
PendingIntent.getActivity
to an Intent Resolution Activity which has no UI and is Transparent so is not shown to the user and it re routes user to the right place.
m
@Rahul Rawat thank you very much! Look like you are right. Some of our actions should use PendingIntent.getActivity instead of PendingIntent.getBroadcas, because of only starting activity in the BroadcastReceiver. But there are some actions that need to check whether should start an activity or not and we are implementing this in BroadcastReceiver. I'm thinking about starting a transparent activity to handle this (although this looks like a workaround). Thank you for your comments.
👍 1
r
Glad I could help. Please remember to finish the activity when your redirection or work is done so the activity doesn’t remain on backstack or become a ghost which prevents user’s interaction with their screen.
👍 1
m
Certainly. Thanks for your remind.
FYI: I found out the exact reason. It's not because of the restrictions on when apps can start activities when the app is running in the background from Android 10. It's because of the Notification trampoline restrictions on Android 12. Details here: https://developer.android.com/about/versions/12/behavior-changes-12#notification-trampolines
r
So it seems my way has become the official way then 😁
m
Yes. Their suggest is the same as your 😁