android 12,如何替换通知蹦床
拥有拦截推送通知的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您最好直接从
Notification
启动Activity
。然后,Activity
可以进行分析并找出需要启动的Activity
,然后委托给它。作为替代方案,启动的Activity
可以将广播Intent
发送到可以完成这项工作的BroadcastReceiver
。注意:如果您直接从
通知
启动活动
,则不是蹦床。当您直接从Notification
启动Service
或BroadcastReceiver
时,就会发生蹦床,并且该组件然后启动 <代码>活动。这个想法是用户需要控制屏幕上弹出的内容。如果他点击通知,他预计屏幕上会出现一些内容,如果您的Service
启动Activity
,这可能随时发生并可能会打断他。You are better off launching an
Activity
directly from theNotification
. TheActivity
could then do the analytics and figure out whatActivity
needs to be launched and then delegate to that. As an alternative, the launchedActivity
could send the broadcastIntent
to yourBroadcastReceiver
which could do the work.NOTE: If you launch an
Activity
directly from aNotification
, that is not a trampoline. The trampoline occurs when you launch aService
or aBroadcastReceiver
directly from theNotification
and that component then launches anActivity
. 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 yourService
launches anActivity
, that can happen at any time and could possibly interrupt him.