android 12,如何替换通知蹦床

发布于 2025-01-09 17:00:20 字数 1749 浏览 0 评论 0原文

拥有拦截推送通知的android sdk,并一直使用通知蹦床来进一步打开结束活动。对于深度链接,使用此 sdk 的应用程序将打开配置的深度链接处理程序活动。

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) {}
    }
}

通知蹦床在 Android 12 中不再工作。 sdk 中需要通知蹦床来拦截点击并执行诸如记录分析事件之类的操作;单击通知上的 Action 按钮时关闭通知抽屉等。并且此 SDK 不知道应用程序可能配置哪些活动来处理深层链接。

使用虚拟活动来替换蹦床是可行的,但感觉不太对劲,即打开活动并在里面打开另一个活动,然后完成这个活动。

当 Android 12 对通知 Tramoline 进行限制时,它是否建议替换像这里这样的用例?还没找到呢

首先拦截推送通知点击然后打开活动的建议新解决方案是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

碍人泪离人颜 2025-01-16 17:00:20

您最好直接从 Notification 启动 Activity。然后,Activity 可以进行分析并找出需要启动的 Activity,然后委托给它。作为替代方案,启动的 Activity 可以将广播 Intent 发送到可以完成这项工作的 BroadcastReceiver

注意:如果您直接从通知启动活动,则不是蹦床。当您直接从 Notification 启动 ServiceBroadcastReceiver 时,就会发生蹦床,并且该组件然后启动 <代码>活动。这个想法是用户需要控制屏幕上弹出的内容。如果他点击通知,他预计屏幕上会出现一些内容,如果您的 Service 启动 Activity,这可能随时发生并可能会打断他。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文