PendingIntent Notification about PendingIntent

PendingIntent builds PendingIntent with the following code:

Build the PendingIntent that contains the return stack

TaskStackBuilder

Sample code for the official website is as follows:

    <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>
Copy the code
    // 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);
Copy the code

Problems in the project

This is written so that an Activity that is started by notification will be displayed on the stack when the back key is clicked

The codes of these two places are basically the same. I also wrote PendingIntent in this way in my app, but there was an error. The code of Notification in this app is written in Service, and I don’t know whether the above writing method is related to this.

  1. When notifications are grouped, clicking on a notification group launches the default LauncherActivity (what I need is to expand the notification group).
  2. After the notification group is expanded, clicking on a single notification removes all activities that already exist on the stack and starts the Activity in the PendingIntent. Clicking on a single notification will start the Activity added to the manifestandroid:parentActivityNameThe Activity. (The Activity will be activated even if it was not at the top of the stack before)

The Android example solves the problem

Later, go to the official website to download the example of Android notification project in Github. In the example code, you can see the following formula.

// 3. Set the main intent of the notification.
Intent mainIntent = new Intent(this, BigPictureSocialMainActivity.class);

When you create your Intent, you need to consider the fallback state, that is, what happens when your Activity starts and the user presses the back button.
// There are two options:
// 1. Regular Activity -- You are starting an activity that is part of the normal workflow of your application.

// 2. Special Activity - The user only sees the activity if it starts with a notification.
// In a sense, activities extend notifications by providing information that is difficult to display in the notifications themselves.

// Even if this example's MainActivity is not linked to the Activity, the Notification starts directly,
That is, it is not part of the normal workflow, a social app will usually link to a single post as part of the workflow, so we will follow option 1.
For an example of option 2, see the BIG_TEXT_STYLE example.

// For more information, check out our development article:
// https://developer.android.com/training/notify-user/navigation.html

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Add the back stack.
stackBuilder.addParentStack(BigPictureSocialMainActivity.class);
// Add the Intent to the top of the stack.
stackBuilder.addNextIntent(mainIntent);
// Get the PendingIntent that contains the entire back stack.
PendingIntent mainPendingIntent = PendingIntent.getActivity(this.0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Copy the code

After modification, the program is normal.

Subsequently, the following formula was tested:

stackBuilder.addParentStack(MessageDetailNewActivity.class);
stackBuilder.addNextIntent(messageIntent);
// This method calls the first two lines, so the effect is the same
//stackBuilder.addNextIntentWithParentStack(messageIntent);
Copy the code

The point is that the project originally used this approach

PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Copy the code

In my app using the writing appeared the mistake, but the website document and a net friend, all is the use of the TaskStackBuilder. GetPendingIntent () method, I don’t know whether my advice is created in the service, Can only use normal call PendingIntent. GetActivity (), because the project time, there is no notification created in the Activity to test this kind of situation, there is record, write demo test later.