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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13581574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:18:01  来源:igfitidea点击:

Android - open or restart app after clicking push notification using flag activities

javaandroidpush-notificationgoogle-cloud-messaging

提问by DijkeMark

I am using push notifications in Android. When I receive a push notification, I want to open the application if it is still running, else it should open a new instance of it.

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

I am using

我在用

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

But when I receive a push notification now and I click on it, nothing happens.

但是,当我现在收到推送通知并单击它时,没有任何反应。

How can I still achieve this by using flagIntents?

我怎样才能通过使用 flagIntents 来实现这一点?

回答by David Wasser

You need to set the Intent flags on the Intent. You were specifying them in the call to get a PendingIntent. Try this:

您需要在Intent. 您在调用中指定它们以获取PendingIntent. 试试这个:

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);

回答by suraj mahajan

set following things in your Android Mainfest file

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

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

回答by CoolMind

In my case a running application restarted every time I clicked a push notification. But I don't want to restart the application.

就我而言,每次单击推送通知时,正在运行的应用程序都会重新启动。但我不想重新启动应用程序。

In PendingIntentI have an intent for needed Activity1 that called Activity2. A bug was in these lines:

PendingIntent我有一个名为 Activity2 的所需 Activity1 的意图。这些行中有一个错误:

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)