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



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

我正在使用

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

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

我怎样才能通过使用标志意图来实现这一点?

您需要

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

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

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

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

PendingIntent中,我对所需的活动 1 有一个意图,称为活动 2。这些行中存在一个错误:

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)

最新更新