Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android-单击使用标志活动的推送通知后打开或重新启动应用程序_Java_Android_Push Notification_Google Cloud Messaging - Fatal编程技术网

Java Android-单击使用标志活动的推送通知后打开或重新启动应用程序

Java Android-单击使用标志活动的推送通知后打开或重新启动应用程序,java,android,push-notification,google-cloud-messaging,Java,Android,Push Notification,Google Cloud Messaging,我在Android中使用推送通知。当我收到推送通知时,如果应用程序仍在运行,我希望打开它,否则它应该打开它的新实例 我正在使用 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVIT

我在Android中使用推送通知。当我收到推送通知时,如果应用程序仍在运行,我希望打开它,否则它应该打开它的新实例

我正在使用

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 
    Intent.FLAG_ACTIVITY_CLEAR_TOP | 
    Intent.FLAG_ACTIVITY_SINGLE_TOP | 
    Intent.FLAG_ACTIVITY_NEW_TASK);
但当我现在收到推送通知并单击它时,什么也没有发生


如何通过使用FlagIntent实现这一点?

您需要在
Intent
上设置Intent标志。您在调用中指定了它们以获取一个
pendingent
。试试这个:

notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
    Intent.FLAG_ACTIVITY_SINGLE_TOP | 
    Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
    notificationIntent, 0);

在Android Mainfest文件中设置以下内容

android:noHistory="true"
 android:launchMode = "singleTop"

在我的例子中,每次单击推送通知时,运行中的应用程序都会重新启动。但是我不想重新启动应用程序

pendingent
中,我有一个需要的活动1的意图,该活动称为活动2。这些行中有一个bug:

val intent = Intent(context, Activity2::class.java)
// Remove this line:
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
context.startActivity(intent)

是的,这个有效。Ecpet现在我有另一个问题。是否有一种方法可以执行onCreate()中的代码,因为它包含推送通知必须执行的代码。现在不调用onCreate,因此代码不会被执行。如果不设置标志
Intent.flag\u ACTIVITY\u SINGLE\u TOP
,则活动的当前实例将被销毁,并将创建一个新实例(从而调用
onCreate()
)。这对我不起作用…您必须为此活动在清单中设置特定的启动模式吗?@Tooroop不,您不需要。什么不起作用?你能说得更具体点吗?看来我解决了。如果同时设置Intent.Intent.FLAG\u ACTIVITY\u SINGLE\u TOP或launchMode=“singleTop”甚至btoh,则不会调用onNewIntent()方法。我添加了:Intent Intent=新Intent(this,MainActivity.class);intent.addFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP);在清单中,我设置了launchMode=“singleTask”。现在它正如期工作为什么
android:noHistory=“true”
android:launchMode=“singleTop”