知方号

知方号

从通知启动 Activity  

从通知启动 Activity 时,您必须保留用户的用户的预期导航体验点按“返回”按钮必须让用户返回从应用的正常工作流转到主屏幕,以及打开“最近使用的应用”必须将 activity 显示为单独的任务。保留此导航请在全新任务中启动 activity。

下文介绍了设置通知点按行为的基本方法创建基本通知。本页介绍了如何设置PendingIntent,您的通知的操作,因此它会创建新任务,并返回堆栈。具体做法取决于您要启动的 activity 类型:

常规 Activity这类 Activity 是应用的正常用户体验流程的一部分。时间当用户从通知到达 activity 时,新任务必须包含完整的返回堆栈,让用户能够通过点按“返回”按钮进行导航应用层次结构的更上层。特殊 Activity只有当 Activity 从通知启动时,用户才可以看到此类 Activity。在此 activity 通过提供很难显示在通知本身中此活动不需要返回堆栈。设置常规 Activity PendingIntent

如需从通知中启动常规 activity,请设置 PendingIntent使用TaskStackBuilder以便创建新的返回堆栈,如下所示。

定义应用的 Activity 层次结构

通过添加android:parentActivityName属性为每个 元素。请参阅以下示例:

...构建包含返回堆栈的 PendingIntent

如需启动包含 activity 返回堆栈的 activity,请创建一个TaskStackBuilder 的实例并调用addNextIntentWithParentStack(),将 Intent 传递给您想要启动的 activity。

只要按照说明为每个 activity 定义父 activity您可以调用getPendingIntent()以接收包含整个返回堆栈的 PendingIntent。

Kotlin// Create an Intent for the activity you want to start.val resultIntent = Intent(this, ResultActivity::class.java)// Create the TaskStackBuilder.val resultPendingIntent: PendingIntent? = TaskStackBuilder.create(this).run { // Add the intent, which inflates the back stack. addNextIntentWithParentStack(resultIntent) // Get the PendingIntent containing the entire back stack. getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)}Java// Create an Intent for the activity you want to start.Intent resultIntent = new Intent(this, ResultActivity.class);// Create the TaskStackBuilder and add the intent, which inflates the back// stack.TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);stackBuilder.addNextIntentWithParentStack(resultIntent);// Get the PendingIntent containing the entire back stack.PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

如有必要,您可以通过调用IntentTaskStackBuilder.editIntentAt()。有时需要这样做,以确保返回堆栈中的 activity在用户导航到时显示有意义的数据。

然后,您可以像往常一样将 PendingIntent 传递到通知中:

Kotlinval builder = NotificationCompat.Builder(this, CHANNEL_ID).apply { setContentIntent(resultPendingIntent) ...}with(NotificationManagerCompat.from(this)) { notify(NOTIFICATION_ID, builder.build())}JavaNotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);builder.setContentIntent(resultPendingIntent);...NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);notificationManager.notify(NOTIFICATION_ID, builder.build());设置特殊 Activity PendingIntent

因为从通知启动的特殊 activity 不需要返回您可以调用 PendingIntent 来创建getActivity()。不过,请在清单中定义适当的任务选项。

在清单中,将以下属性添加到 元素。 android:taskAffinity="" 与 FLAG_ACTIVITY_NEW_TASK 标记,请将此属性留空,以确保 此 activity 不会进入应用的默认任务。不限 具有应用默认相似性的现有任务不会 。 android:excludeFromRecents="true" 将新任务从“最近使用的应用”屏幕中排除,以便用户 就无法意外地返回至该网域

具体可见以下示例:

构建并发出通知: 创建一个 Intent,用于启动 Activity。 将 Activity 设置为在新的空任务中启动,具体方法如下: 呼叫 setFlags() 使用标记FLAG_ACTIVITY_NEW_TASK和 FLAG_ACTIVITY_CLEAR_TASK。 通过调用以下方法来创建 PendingIntent: getActivity()。

具体可见以下示例:

Kotlinval notifyIntent = Intent(this, ResultActivity::class.java).apply { flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK}val notifyPendingIntent = PendingIntent.getActivity( this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)JavaIntent notifyIntent = new Intent(this, ResultActivity.class);// Set the Activity to start in a new, empty task.notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);// Create the PendingIntent.PendingIntent notifyPendingIntent = PendingIntent.getActivity( this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); 像往常一样将 PendingIntent 传递到通知:Kotlinval builder = NotificationCompat.Builder(this, CHANNEL_ID).apply { setContentIntent(notifyPendingIntent) ...}with(NotificationManagerCompat.from(this)) { notify(NOTIFICATION_ID, builder.build())}JavaNotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);builder.setContentIntent(notifyPendingIntent);...NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);notificationManager.notify(NOTIFICATION_ID, builder.build());

如需详细了解各种任务选项以及返回堆栈的方式请参见任务和返回堆栈。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lizi9903@foxmail.com举报,一经查实,本站将立刻删除。