PendingIntent from notification and application history conflicts
-
23-09-2019 - |
Question
I'm creating a notification with something similar to the following:
Intent ni = new Intent(this, SomeActivity.class);
ni.putExtra("somestring", "somedata");
PendingIntent contentIntent =
PendingIntent.getActivity(this, 0, ni,
PendingIntent.FLAG_UPDATE_CURRENT|PendingIntent.FLAG_ONE_SHOT);
Context context = getApplicationContext();
notification.setLatestEventInfo(context,
res.getString(R.string.app_name),
text, contentIntent);
The key here is the extra data on the Intent for the notification. Once I click on the notification and it brings up SomeActivity, it brings up the activity and the extra data is available.
However, if I exit the app, hold the home button until the recent activities list comes up, and choose to open the app again, the extra data is still passed. Is there a way I can make this data get passed only if the app is opened via the Notification, and not from the recent activities list?
Solution
Looks like I can check to see if the Activity was launched from the recent activities dialog with this:
if((getIntent().getFlags() &
Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
// ignore extras
}
else {
// do not ignore extras
}
I don't know if this is the best way, but it does work.
OTHER TIPS
Use a different Intent
. For example, you could add an <intent-filter>
for some custom action string, and use that for your Notification
-based PendingIntent
. In other words, you can't change the most-recent activities dialog, but you can change your side of the conflict.
Try with:
int NOTIFICATION_CODE = 1;
PendingIntent contentIntent =
PendingIntent.getActivity(this,
NOTIFICATION_CODE,
ni,
PendingIntent.FLAG_UPDATE_CURRENT |
PendingIntent.FLAG_ONE_SHOT
);
Change NOTIFICATION_CODE value for every notification.