android - 当收到多个 Android 通知时,只有最新的一个上的操作按钮有效

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

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

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

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

提前致谢。

最佳答案

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

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

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

关于android - 当收到多个 Android 通知时,只有最新的一个上的操作按钮有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47841478/

相关文章:

android - XML android 中带有圆底的背景图像

android - 在 RadioGroup 上调用 clearCheck() 后调用 RadioGroup 的 setOnCheckedChangeListener 两次

javascript - Ionic - 向同一组中的用户推送通知

java - 如何在三层、Web 服务后端、桌面应用程序中实现异步通知?

启动器图标上的 Android 通知

ios - 预定的本地通知不规则

java - 恢复上一个 fragment onBackPressed()?

Robolectric 2.2 中的 android.content.res.Resources$NotFoundException

当应用程序被杀死时,iOS 不会收到推送通知

iphone - 我如何从用户未激活/触摸的应用程序外部收到的推送通知中接收信息?