Track notifications click without trampolines (forbidden in Android 12)

984 Views Asked by At

Android 12 has forbidden the so called notification trampolines: https://developer.android.com/about/versions/12/behavior-changes-12#notification-trampolines

Currently I'm tracking notification click events trough analytics tools but making use of a "trampoline", a broadcast receiver which send the event before the actual notification Intent to open a specific Activity.

Reading here and there I see that an alternative to that approach, which would avoid trampolines as well, consists in populating the Intent extras with the information required to send the analytics event. But here I see 2 issues:

  1. This logic shall be replicated on all the involved Activities, so it's quite hard to make it solid to avoid mistakes/bugs
  2. It's not possible, AFAIK, to cleanup the Intent extras on Activity re-creation, meaning that, even if you remove the intent extras required to send the event, if the same activity is re-created (i.e. using the "don't keep activities" dev option), the original Intent extras are used, resulting in multiple events for a single real notification click.

Here is the function I use to consume the intent extra:

fun consumeClickEventIfAny(intent: Intent) {
    intent.extras?.let { extras ->
        if (extras.containsKey(EXTRA_ANALYTICS_FOR_CLICK)) {
            ... trigger analytics event here
            extras.remove(EXTRA_ANALYTICS_FOR_CLICK)
            intent.replaceExtras(extras)
        }
    }
}

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

0

There are 0 best solutions below