通知 - 点击引发错误的PendingIntent

pro*_*m85 7 notifications android android-intent android-notifications

我有以下内容意图的通知:

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);
Run Code Online (Sandbox Code Playgroud)

以及具有以下行动意图的一项行动:

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);
Run Code Online (Sandbox Code Playgroud)

单击通知时,将触发关闭意图.为什么?

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

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);
Run Code Online (Sandbox Code Playgroud)

W0r*_*0le 14

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

我认为问题是他们共享相同的更新号码:

这是创建:

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);
Run Code Online (Sandbox Code Playgroud)

然后,创建这个新的:

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);
Run Code Online (Sandbox Code Playgroud)

它类似于前一个,它被设置PendingIntent.FLAG_UPDATE_CURRENT.所以,第二个更新第一个.

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

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