我的Android通知有操作按钮。当我连续发送两个通知时,点击第一个上的操作按钮没有任何效果(操作处理程序代码将不会执行),但点击最近通知上的操作按钮可以按预期工作。
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);
}
最近的通知不会取消之前的通知,这是我们想要的。但是,最近的一个似乎是禁用以前通知中的操作按钮。
有没有人有想法可能导致它?我一直在玩意图标志,但到目前为止似乎没有任何区别。
提前致谢。
在操作响应待处理意图中,确保请求代码对于每个通知都是唯一的:
PendingIntent.getBroadcast(context, requestCode, cancel, PendingIntent.FLAG_CANCEL_CURRENT);
请注意,此代码中的requestCode必须是唯一编号。如果它与之前的通知操作意图相同,则最终将覆盖前一个通知操作意图。