I'm trying to present a notification which is handled by a special activity which is not in the normal application flow, and trying to get the back stack handling "correct," meaning:
So far, the code I'm using to present the notification is:
/**
* Show (or update) a notification for the current message set.
*
* @param showNotification true to use a high priority notification which will be immediately
* displayed (as opposed to just added to the status bar)
*/
private void createOrUpdateNotification(boolean showNotification) {
Message oldest = messages.get(0);
Message newest = messages.get(messages.size() - 1);
// Create the notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
// Set notification data and appearance
.setContentTitle(title)
.setContentText(newest.message)
.setSmallIcon(smallIcon)
.setWhen(newest.when)
.setColor(ContextCompat.getColor(context, R.color.primary_color))
// Set notification options
.setAutoCancel(true)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setPriority(showNotification ? NotificationCompat.PRIORITY_HIGH : NotificationCompat.PRIORITY_LOW)
.setDefaults(NotificationCompat.DEFAULT_VIBRATE)
.setOnlyAlertOnce(!showNotification);
// Set up the action if the first (oldest) message has an intent builder
if(oldest.intent != null) {
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context.getApplicationContext());
stackBuilder.addNextIntent(oldest.intent);
builder.setContentIntent(stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT));
}
NotificationManagerCompat.from(context).notify(notificationId, builder.build());
Log.i("notification created");
}
For clarification, Message.intent
is a single intent, configured to open the notification handling activity.
My problem is that if the application is currently running and open when the notification is opened, the application is closed and the notification presented on an empty stack and the application's back stack is cleared out.
My understanding is that the desired behavior should be automatic if the content intent is a pending intent containing a single intent, which is the case here.
What am I missing?
TaskStackBuilder.create will launch a fresh task stack.
Set the content intent like this instead:
builder.setContentIntent(
PendingIntent.getActivity(
context,
0,
oldest.intent,
PendingIntent.FLAG_UPDATE_CURRENT
));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With