Java Android通知点击打开应用程序仅在第一次生效

Java Android通知点击打开应用程序仅在第一次生效,java,android,android-intent,notifications,android-pendingintent,Java,Android,Android Intent,Notifications,Android Pendingintent,我有一个带有通知的前台服务。我创建如下通知: Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setOngoing(true) .setContentTitle("App name") .setContentText(Utility.getNotificationContentText())

我有一个带有通知的前台服务。我创建如下通知:

    Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setOngoing(true)
            .setContentTitle("App name")
            .setContentText(Utility.getNotificationContentText())
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setContentIntent(pendingIntent)
            .setOnlyAlertOnce(true)
            .build();
在通知中,我设置了一个挂起的意图,如下所示:

    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
当我第一次单击通知时,它会按预期工作。它会打开应用程序。但是,当我第二次单击通知时,什么也没有发生。

无论如何,我打开MainActivity.class,但我的应用程序有不同的片段。我想打开一个特定的片段,但我不确定是否可以将FragmentName.class传递到intent中


获得通知的任何帮助每次单击即可生效吗?

设置
PendingInt
的以下标志,以便您可以使用新的
PendingInt
更新当前是否有挂起的意图

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 
              PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

设置
pendingtent
的以下标志,以便您可以使用新的
pendingtent
更新当前的挂起意图

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 
              PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

我设法把它修好了。问题是我更新通知的目的是使用错误的类。

我成功地修复了它。问题是我更新通知的目的是使用错误的类。

谢谢你的回答,但这不起作用。谢谢你的回答,但这不起作用。