在通知重新启动应用程序接收重新启动、应用程序、通知

2023-09-06 17:20:58 作者:你丑没事我瞎

我已经开发了具有从服务器接收通知的功能的应用程序。

I have developed an application which has the functionality of receiving notifications from server.

现在的问题是,当我点击,我收到了一个通知,它的打开我的应用程序本身的新实例。

The problem is, when I click on a notification that I have received, it opens a new instance of my application itself.

此行​​为是确定的,如果我的应用程序是不是在前台的 的,但如果是我尝试打开一个通知,我的应用程序的一个新实例被创建,因此重叠previously打开该应用程序的实例。

This behavior is ok, if my app is not in the foreground, but if it is and I try to open a notification, a new instance of my app is created and thus overlapping the previously opened instance of the app.

我不希望这样的事情发生,所以当我点击的通知,如果我的应用程序是在前台的我必须关闭的,打开一个新的实例。

I don't want this to happen, so when I click on the notification if my app is in the foreground I have to close that and open a new instance.

我应该如何重写通知的单击事件?

How should I override the notification's click event?

推荐答案

您需要做一些神奇的IntentFlags。 尝试不同的标志添加到您的意图。

you need to do some magic with the IntentFlags. try to add different flags to your intent.

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);