Remove data from notification intent

2019-01-03 03:15发布

I have issue in intent of my launcher activity.Scenerio is: 1. Send intents form notification service to my launcher activity

PendingIntent contentIntent = PendingIntent.getActivity(this, TripLoggerConstants.PENDING_TRIPS_NOTIFICATION_ID, new Intent(this, MainActivity.class).putExtra("is_log", true), Intent.FLAG_ACTIVITY_CLEAR_TOP);

2. In my MainActivity i getting this intent. code is:

if(this.getIntent().getExtras()!=null){

        boolean isLogNewTripScreen  = (boolean)this.getIntent().getExtras().getBoolean("is_log");

    }
    }

3. this work fine but when i come from notification service,but when i launch from not notification service ,that data in intentis still there.How can i remove that data from intent.

2条回答
在下西门庆
2楼-- · 2019-01-03 04:02

EDIT: I've created a sample application to test this problem and possible solutions. Here are my findings:

If you launch your app from a notification with extras and then later return to your app by selecting it from the list of recent tasks, Android will launch the app again the same way it was launched from the notification (ie: with the extras). This is either a bug or a feature, depending on who you ask.

You'll need to add additional code to deal with this situation. I can offer 2 suggestions:

1. Use FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS

When you create your notification, set the flag Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS in the Intent. Then, when the user selects the notification and launches the app from the notification, this will not create an entry for this task in the list of recent tasks. Also, if there was an entry in the list of recent tasks for this application, that entry will also be removed. In this case, it will not be possible for the user to return to this task from the list of recent tasks. This solves your problem by removing the possibility that the user launches the app from the list of recent tasks (but only when the app has been launched from the notification).

2. Detect FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY

When the user launches your app from the list of recent tasks, Android sets the flag Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY in the Intent that is passed to onCreate() of your launch activity. You can detect the presence of this flag in onCreate() and then you know that the app has been launched from the recent tasks list and not from the notification. In this case, you can just ignore the fact that the extras in the Intent still contain data.

Choose the solution that best suits the workflow for your application. And thanks for the question, this was an interesting challenge to solve :-)


Additional information:

You are creating the PendingIntent incorrectly. You are calling

PendingIntent contentIntent = PendingIntent.getActivity(this,
        TripLoggerConstants.PENDING_TRIPS_NOTIFICATION_ID,
        new Intent(this, MainActivity.class).putExtra("is_log", true),
        Intent.FLAG_ACTIVITY_CLEAR_TOP);

You are passing Intent.FLAG_ACTIVITY_CLEAR_TOP as the 4th parameter to getActivity(). However, that parameter should be PendingIntent flags. If you want to set FLAG_ACTIVITY_CLEAR_TOP on the Intent, you need to do it this way:

PendingIntent contentIntent = PendingIntent.getActivity(this,
        TripLoggerConstants.PENDING_TRIPS_NOTIFICATION_ID,
        new Intent(this, MainActivity.class).putExtra("is_log", true)
        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP), 0);
查看更多
SAY GOODBYE
3楼-- · 2019-01-03 04:03

I noticed that using fragments. I read a QR Code in Activity A that opens fragment 1, send its content to a webservice and if goes right, replace it with fragment 2. When user press back, the onBackPressed in Activity A call finish. If user select the app again in the list, it was opening fragment 1 instead of fragment 2.

I solved that checking in onBackPressed if extra contains a field indicating that fragment 2 was already opened. If true, moveTaskToBack(true) is called instead of finish()

Activity A

@Override
public void onBackPressed() {
 Bundle extras = getIntent().getExtras();
 if(extras.containsKey(Constants.TICKET_DONT_SHOW_QRCODE_SCREEN)){
    moveTaskToBack(true);
 }else {
    finish();
 }
}

Fragment 2

Intent mainIntent = getActivity().getIntent();
mainIntent.putExtra(Constants.TICKET_DONT_SHOW_QRCODE_SCREEN, true);
getActivity().setIntent(mainIntent);
查看更多
登录 后发表回答