从通知启动 Activity

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

创建基本通知中介绍了为通知设置点按行为的基本方法。本页介绍了如何为通知的操作设置 PendingIntent,以便它创建新的任务和返回堆栈。具体操作方式取决于您要启动的 activity 类型:

常规 Activity
这类 Activity 是应用的正常用户体验流程的一部分。当用户从通知转到 activity 时,新任务必须包含完整的返回堆栈,以便用户点按返回按钮在应用层次结构中向上导航。
特殊 Activity
只有当 Activity 从通知启动时,用户才可以看到此类 Activity。从某种意义上讲,此 activity 通过提供通知本身难以显示的信息来扩展通知界面。此 activity 不需要返回堆栈。

设置常规 Activity PendingIntent

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

定义应用的 Activity 层次结构

通过向应用清单文件中的每个 <activity> 元素添加 android:parentActivityName 属性,定义 activity 的自然层次结构。请参阅以下示例:

<activity
    android:name=".MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<!-- MainActivity is the parent for ResultActivity. -->
<activity
    android:name=".ResultActivity"
    android:parentActivityName=".MainActivity" />
    ...
</activity>

构建包含返回堆栈的 PendingIntent

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

只要您按照前文所述为每个 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);

如有必要,您可以通过调用 TaskStackBuilder.editIntentAt() 向堆栈中的 Intent 对象添加参数。有时必须这样做,才能确保返回堆栈中的 activity 在用户导航到该 activity 时显示有意义的数据。

然后,您可以照常将 PendingIntent 传递给通知:

Kotlin

val builder = NotificationCompat.Builder(this, CHANNEL_ID).apply {
    setContentIntent(resultPendingIntent)
    ...
}
with(NotificationManagerCompat.from(this)) {
    notify(NOTIFICATION_ID, builder.build())
}

Java

NotificationCompat.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 不需要返回堆栈,因此您可以通过调用 getActivity() 来创建 PendingIntent。不过,请在清单中定义相应的任务选项。

  1. 在清单中,将以下属性添加到 <activity> 元素中。
    android:taskAffinity=""
    与在代码中使用的 FLAG_ACTIVITY_NEW_TASK 标志结合使用,将此属性设置为空白,以确保此 activity 不会进入应用的默认任务。具有应用默认亲和性的任何现有任务都不会受到影响。
    android:excludeFromRecents="true"
    从“最近使用的应用”屏幕中排除新任务,以免用户意外返回该任务。

    具体可见以下示例:

    <activity
        android:name=".ResultActivity"
        android:launchMode="singleTask"
        android:taskAffinity=""
        android:excludeFromRecents="true">
    </activity>
    
  2. 构建并发出通知:
    1. 创建一个可启动 ActivityIntent
    2. 通过使用 FLAG_ACTIVITY_NEW_TASKFLAG_ACTIVITY_CLEAR_TASK 标志调用 setFlags(),将 Activity 设置为在新的空任务中启动。
    3. 通过调用 getActivity() 创建 PendingIntent

    具体可见以下示例:

    Kotlin

    val 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
    )
    

    Java

    Intent 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
    );
    
  3. 像往常一样将 PendingIntent 传递给通知:

    Kotlin

    val builder = NotificationCompat.Builder(this, CHANNEL_ID).apply {
        setContentIntent(notifyPendingIntent)
        ...
    }
    with(NotificationManagerCompat.from(this)) {
        notify(NOTIFICATION_ID, builder.build())
    }
    

    Java

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
    builder.setContentIntent(notifyPendingIntent);
    ...
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(NOTIFICATION_ID, builder.build());
    

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