3

Due to android 12 the Notification trampoline restrictions leads to a big problem in my project. I'm developed an SDK and it's used to show local notifications. My problem is that I'm using the notification is to show a banner or open a web view inside the application. For the Notification trampoline restrictions I need to specify the notification click event using pending intent. My issue is that from the SDK side I find out the application bundle Id and lunch the MainActivity, in every time I'm tapping the notification the application is relaunched even if the application is in the background or foreground.

I need to know about the following

  1. how can I know when the application is alive in the foreground or background
  2. if it exists need to know which activity is at the top of the stack

Did you face the same or similar issue? How did you solve it? Any help would be very appreciated. Thank you!

jerald jacob
  • 613
  • 1
  • 4
  • 18

1 Answers1

6

I'm using a transparent activity to handle this issue. all the notification related works are handled in the transparent activity.

Intent intent = new Intent(mContext, NotificationActivity.class);
intent.putExtra("notification", parseInt(this.mActionDetail.getNotifyId()));
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
notificationManager.notify(parseInt(this.mActionDetail.getNotifyId()), builder.build());

create a transparent activity NotificationActivity.class

jerald jacob
  • 613
  • 1
  • 4
  • 18
  • 3
    Can you elaborate your answer? Post an example? Thanks – AndroidRuntimeException Oct 07 '21 at 16:05
  • 4
    This may be the way I solve my issue, but it seems like an anti-pattern ... a workaround to the problem of slow app launches from notifications. – dhaag23 Nov 04 '21 at 17:52
  • @dhaag23 can you please suggest any other way? – jerald jacob Nov 09 '21 at 04:26
  • 1
    I wish. We have some complicated logic to determine the correct Activity to show once we get the PendingIntent, so I ended up adding the transparent Activity (I even called it NotificationTrampolineActivity). – dhaag23 Nov 12 '21 at 23:59
  • 2
    I also ended up doing the same, since doing it "Google's way" would be an immense refactoring to existing logic and it's just not worth it. If they make this solution impractical in a future SDK, we might be forced to do deep refactoring. – SirKnigget Feb 20 '22 at 09:43
  • 1
    i have same problem. but i don't know start activity from action notification in broadcast receiver. – MKD May 13 '22 at 07:20
  • you can use a transparent activity first to handle notification above android 11. then work out your logic inside of that particular activity. For example if you want a parameter value while clicking the notification you can pass it on intent then write your logic with this value. all the actions are performed in the transparent activity – jerald jacob May 14 '22 at 07:02