android 12, how replace the notification trampolines

Viewed 1340

Having android sdk which intercept the push notification, and has been using notification trampoline to further open the end activity. In the case of deeplink the app who uses this sdk will open the configured deeplink handler activity.

Snippet for the trampoline:

public class NotificationTrampolineReceiver extends BroadcastReceiver {
@Override
    public void onReceive(final Context context, final Intent intent) {
        final PendingResult asyncResult = goAsync();
        ExecutorService executor = Executors.newSingleThreadExecutor();
        asycTask(executor, new Runnable() {
            @Override
            public void run() {
                String urlStr = getStringExtra(intent, PUSH_URL);
                if (urlStr != null) {
                    var intent2: Intent = Intent(Intent.ACTION_VIEW, Uri.parse(urlStr));
                    if (intent2 != null) {
                        intent2.addFlags(FLAG_ACTIVITY_NEW_TASK);
                        intent2.addFlags(FLAG_ACTIVITY_BROUGHT_TO_FRONT);
                        context.startActivity(intent2);
                        logAnalytics(intent, Message.MessageAction.OPEN);                   
                     }
                }
                asyncResult.finish();
            }
        });
    }

    void asycTask(ExecutorService executor, final Runnable task) {
        try {
            executor.execute(task);
        } catch (Throwable ex) {}
    }
}

The notification trampolines is not working in Android 12 anymore. The notification trampolines is needed in the sdk to intercept the click and do something like to log analytics event; closing the notification drawer when clicking at the Action button on the notification, etc. And this sdk does not know what activities the app may configure to handle the deeplinks.

Using a dummy activity to replace the trampoline would work, but not feel right, i.e. open the activity and inside to open another one then finish this one.

When android 12 puts restriction on the notification tramoline, does it suggest a replacement for the use case like the one here? Haven't find one.

What is the suggested new solution for intercepting the push notification tap first and then open the activity?

1 Answers

You are better off launching an Activity directly from the Notification. The Activity could then do the analytics and figure out what Activity needs to be launched and then delegate to that. As an alternative, the launched Activity could send the broadcast Intent to your BroadcastReceiver which could do the work.

NOTE: If you launch an Activity directly from a Notification, that is not a trampoline. The trampoline occurs when you launch a Service or a BroadcastReceiver directly from the Notification and that component then launches an Activity. The idea is that the user needs to be in control of what pops onto his screen. If he taps a notification, he expects that something will appear on his screen, if your Service launches an Activity, that can happen at any time and could possibly interrupt him.

Related