【问题标题】:When multiple Android notifications are received, only the action buttons on the latest one work当收到多个 Android 通知时,只有最新的一个上的操作按钮有效
【发布时间】:2017-12-16 00:32:27
【问题描述】:

我的 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);
}

最近的通知不会取消之前的通知,这是我们想要的。然而,最近的一个似乎是禁用以前通知上的操作按钮。

有人知道是什么原因造成的吗?我一直在使用意图标志,但到目前为止似乎没有任何区别。

提前致谢。

【问题讨论】:

  • 操作按钮是否出现在所有通知中??
  • @Xenolion 是的,操作按钮出现在所有通知上。但只有最新通知上的按钮才能被点击并触发逻辑

标签: android push-notification notifications google-cloud-messaging android-notifications


【解决方案1】:

在操作响应待处理意图中,确保每个通知的请求代码都是唯一的:

PendingIntent.getBroadcast(context, requestCode, cancel, PendingIntent.FLAG_CANCEL_CURRENT);

请注意,此代码中的 requestCode 必须是唯一编号。如果它与之前的通知动作意图相同,它将最终覆盖之前的。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,请确保 pendingintent 请求代码不是常量。它必须具有唯一值,否则之前的按钮将被禁用。就我而言,我将通知 ID 保留为 pendingintent 的请求代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-18
      • 1970-01-01
      • 2015-06-26
      • 2016-08-18
      相关资源
      最近更新 更多