Reputation: 1746
My Android notifications have action buttons. When I send two notifications in a row, tapping on the action buttons on the first one doesn't have any effect (action handler code won't execute), but tapping on the action buttons on the most recent notification works as expected.
private void displayNotification(Context context, ChallengeInformation extras) {
/* build the notification */
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.status_bar_icon)
.setContentTitle(context.getString(R.string.push_notification_title))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(getChallengeContextString(extras)))
.setContentText(context.getString(R.string.push_notification_description))
.setAutoCancel(false) // We don't want to cancel when the user clicks
.setPriority(NotificationCompat.PRIORITY_MAX)
.setColor(context.getResources().getColor(R.color.notification))
.setLocalOnly(true) // we handle notifications on Wear separately
.setDefaults(DEFAULTS);
/* set the target of the notification */
PendingIntent challenge =
getChallengePendingIntent(context, extras);
mBuilder.setContentIntent(challenge);
addNotificationActions(mBuilder, context, extras);
challengeTracker.notifyChallenge(extras, context, mBuilder.build());
}
private void addNotificationActions(NotificationCompat.Builder builder, Context context,
ChallengeInformation extras) {
//add buttons to the notification
PendingIntent approveIntent = getResponsePendingIntent(context, extras, true, ACCEPT_REQUEST_CODE);
NotificationCompat.Action approveAction =
new NotificationCompat.Action.Builder(R.drawable.notification_action_approve,
context.getString(R.string.notification_approve_text), approveIntent).build();
NotificationCompat.Action rejectAction =
new NotificationCompat.Action.Builder(R.drawable.notification_action_decline,
context.getString(R.string.notification_reject_text), rejectIntent).build();
builder.addAction(approveAction);
builder.addAction(rejectAction);
}
public static PendingIntent getResponsePendingIntent(Context context, ChallengeInformation info,
boolean approve, int requestCode) {
Intent cancel = new Intent(context, GcmBroadcastReceiver.class);
cancel.setAction(Constants.RESPOND_TO_SERVER);
cancel.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
cancel.putExtras(info.getBundle());
cancel.putExtra(Constants.USER_RESPONSE_ACCEPTED_KEY, approve);
return PendingIntent.getBroadcast(context, requestCode, cancel,
PendingIntent.FLAG_CANCEL_CURRENT);
}
The most recent notification does not cancel out the previous ones, which is what we want. However the most recent one seems to be disabling the action buttons on previous notifications.
Does anyone have ideas what might be causing it? I have been playing with the intent flags, but nothing seems to make a difference so far.
Thanks in advance.
Upvotes: 4
Views: 787
Reputation: 483
I had this same issue make sure that the pendingintent requestcode is not a constant. It must have a unique value or else the previous buttons get disabled. In my case I kept the notification ID as the requestcode of the pendingintent
Upvotes: 0
Reputation: 1746
In the action response pending intent, make sure the request codes are unique for each notification:
PendingIntent.getBroadcast(context, requestCode, cancel, PendingIntent.FLAG_CANCEL_CURRENT);
Note the requestCode in this code has to be a unique number. If it's the same as the previous notification action intent, it will end up overwriting the previous one.
Upvotes: 5