Android 通知重新启动应用程序但想要恢复

问题描述 投票:0回答:7

您好,我能够收到有关我的活动的通知,并且当用户 单击应用程序重新启动的通知。但我只是希望它重新出现而不是重新启动。例如。它是一个网络应用程序,我希望当用户选择通知时它出现在前面......但不刷新网页。我可以捕获这个意图还是我发送了错误的意图?通常,如果我按主页按钮并单击应用程序图标,应用程序就会出现在前面并且不会刷新/重新启动。这就是我想要的行为。有什么想法吗?

 String ns = Context.NOTIFICATION_SERVICE;
 NotificationManager mNotificationManager = (NotificationManager)
                                             getSystemService(ns);
 //2.Instantiate the Notification
 int icon = R.drawable.notification_icon;
 CharSequence tickerText = "My App";  // temp msg on status line 
 long when = System.currentTimeMillis();
 Notification notification = new Notification(icon, tickerText, when);
 notification.flags |= Notification.FLAG_ONGOING_EVENT;
 //3.Define the Notification's expanded message and Intent: 
 Context context = getApplicationContext();
 CharSequence contentTitle = "Notification";
 CharSequence contentText = "My app!"; // message to user
 Intent notificationIntent = new Intent(this, HelloAndroid2.class);
 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
                                                          notificationIntent, 0);
 notification.setLatestEventInfo(context, contentTitle, contentText,
                                                          contentIntent);
 //4.Pass the Notification to the NotificationManager:  
 final int NOTIFICATION_ICON_ID = 1;
 mNotificationManager.notify(NOTIFICATION_ICON_ID, notification);
android background notifications foreground android-pendingintent
7个回答
33
投票

我把它放在清单中

android:launchMode="singleInstance"

这解决了我的问题。另外这个答案我没有尝试过,它暗示了其他感兴趣的事情。


13
投票

如果您必须避免将 Activity 设置为“singleInstance”,请按如下方式设置通知的意图:

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

6
投票

您可以尝试这个 FLAG_ACTIVITY_REORDER_TO_FRONT (文档准确描述了您想要的内容)

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT

Intent notificationIntent = new Intent(this, HelloAndroid2.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

1
投票

我这里有另一个适合我的解决方案:

    //Resume or restart the app (same as the launcher click)
    val resultIntent = Intent(context, MyLauncherActivity::class.java)
    resultIntent.addCategory(Intent.CATEGORY_LAUNCHER)
    resultIntent.setAction(Intent.ACTION_MAIN)
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    val pendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT)
    builder.setContentIntent(pendingIntent)  //builder is the notificationBuilder

0
投票

我建议在这种特殊情况下设置标志 Intent.FLAG_ACTIVITY_SINGLE_TOP ,如果您使用 android:launchMode="singleInstance" ,它会影响您在应用程序中调用其他意图时的行为,例如使用 facebook sdk、paypal sdk 等。

这里的解释很简单: 链接


0
投票

如果有的话,请从 .MainActivity 活动部分的清单中删除它:

android:noHistory="true"

这对于无法在 Android 7 之后的三星设备(和其他设备)上恢复应用程序来说是致命的


0
投票

我尝试了上述方法但没有成功(应用程序不断重新启动),直到我删除:

intent.setPackage(this.getPackageName());

我最近添加了它,看看是否可以修复 Android 14 的通知问题。现在我正在使用:

intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP)

返回到正在运行的计时器,并且很高兴它仍然按预期运行。

© www.soinside.com 2019 - 2024. All rights reserved.