通知 - 单击会触发错误的 PendingIntent

Notification - click fires wrong PendingIntent

我有一个包含以下内容意图的通知:

clickIntent = PendingIntent.getService(
                    getApplicationContext(),
                    Constants.REQUEST_CODE_NOTIFICATION_OTHER,
                    new Intent(getApplicationContext(), MainService.class)
                            .putExtra(Constants.EVENT, Constants.EVENT_TOGGLE_BLACK),
                    PendingIntent.FLAG_UPDATE_CURRENT);

和一个具有以下动作意图的动作:

PendingIntent closeIntent = PendingIntent.getService(
        getApplicationContext(),
        Constants.REQUEST_CODE_NOTIFICATION_OTHER,
        new Intent(getApplicationContext(), MainService.class)
                .putExtra(Constants.EVENT, Constants.EVENT_DISABLE_SERVICE),
        PendingIntent.FLAG_UPDATE_CURRENT);

当我点击通知时,关闭意图被触发。为什么?

以下是我创建通知的方式:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(getString(text))
            .setStyle(new NotificationCompat.BigTextStyle().bigText(getString(details)))
            .setSmallIcon(R.drawable.ic_not)
            .setContentIntent(clickIntent)
            .setOngoing(true);

    boolean showCloseButton = MainApp.getPrefs().enableCloseServiceButtonInNotification();
    if (showCloseButton)
        builder.addAction(R.drawable.ic_close_black_24dp, getString(R.string.stop_service), closeIntent);

这是我的猜测...如果不起作用,我会删除答案...

我认为问题在于它们共享相同的更新编号:

这是创建的:

clickIntent = PendingIntent.getService(
    getApplicationContext(),
    Constants.REQUEST_CODE_NOTIFICATION_OTHER,
    new Intent(getApplicationContext(), MainService.class).putExtra(Constants.EVENT, Constants.EVENT_TOGGLE_BLACK),
    PendingIntent.FLAG_UPDATE_CURRENT);

然后,创建了这个新的:

PendingIntent closeIntent = PendingIntent.getService(
    getApplicationContext(),
    Constants.REQUEST_CODE_NOTIFICATION_OTHER,
    new Intent(getApplicationContext(), MainService.class).putExtra(Constants.EVENT, Constants.EVENT_DISABLE_SERVICE),
    PendingIntent.FLAG_UPDATE_CURRENT);

和上一个类似,设置为PendingIntent.FLAG_UPDATE_CURRENT。 所以,第二个更新第一个。

尝试对不同的 PendingIntents 使用不同的代码。两者都在使用 Constants.REQUEST_CODE_NOTIFICATION_OTHER.

然后,Android将使用该代码来区分它们。