单击通知只工作一次

click Notification just work once

我正在使用自定义通知接收器并在通知栏中显示它们。为了测试,我向 phone 发送了 2 条通知。 我关闭应用程序,点击第一个通知,应用程序打开! 我关闭应用程序,点击第二个通知,没有任何反应!

 private void generateNotification(Context context, String title, JSONObject json, String text) {

        Intent intent = new Intent(context, home.class);

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        NotificationManager mNotifM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setAutoCancel(true)
                        .setContentTitle(title)
                        .setContentText(text)
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(text))
                        .setTicker(text)
                        .setLights(Color.GREEN, 500, 500);

        mBuilder.setContentIntent(contentIntent);

        mNotifM.notify(NOTIFICATION_ID, mBuilder.build());

    }

对每个待定意图使用不同的请求代码或不使用 FLAG_ONE_SHOT 标志

static count = 0;
...

PendingIntent contentIntent = PendingIntent.getActivity(context, count++, intent, PendingIntent.FLAG_ONE_SHOT);

不同的通知有不同的请求代码。看看下面的代码

Random random = new Random();
int NOTIFICATION_ID = random.nextInt(9999 - 1000) + 1000;

 PendingIntent contentIntent = PendingIntent.getActivity(this, NOTIFICATION_ID,
            intent, PendingIntent.FLAG_ONE_SHOT);