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

发布于 2024-10-31 02:04:43 字数 1437 浏览 6 评论 0原文

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

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

Hi I have a been able to get a notification displayed for my activity, and when the user
clicks the notification the app restarts. However I just want it to reappear not restart. eg. it is a web app and I want it to come to the front when the user selects the notification..but not refresh the web page. Can I trap this intent or am I sending the wrong intent? Normally if I press the home button and click on the app icon the app comes to the fore and doesn't refresh/restart. So this is the behaviour I want. Any ideas ?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

小鸟爱天空丶 2024-11-07 02:04:43

我把它放在清单中

android:launchMode="singleInstance"

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

I put this in manifest

android:launchMode="singleInstance"

This fixed the problem for me. Also this answer which I have not tried which allude to other things of interest.

遥远的她 2024-11-07 02:04:43

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

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

If you have to avoid setting your activity to "singleInstance", then set the notification's intent as follows:

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
谈下烟灰 2024-11-07 02:04:43

您可以尝试这个 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);

You can try this FLAG_ACTIVITY_REORDER_TO_FRONT (the document describes exactly what you want to)

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);
温馨耳语 2024-11-07 02:04:43

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

    //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

I have another solution working for me here :

    //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
毅然前行 2024-11-07 02:04:43

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

。这里的解释很简单:
链接

I would recommend to set the flag Intent.FLAG_ACTIVITY_SINGLE_TOP for this particular situation, if you use android:launchMode="singleInstance" it would affect the behaviour when you call other intents in your application, example using facebook sdk, paypal sdk etc..

The explanation is simple right here:
link

简单爱 2024-11-07 02:04:43

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

android:noHistory="true"

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

Remove this from manifest from .MainActivity activity portion if you have it there:

android:noHistory="true"

This is fatal for not being able to resume app on Samsung devices (and others) with Android newer then 7

瑶笙 2024-11-07 02:04:43

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

intent.setPackage(this.getPackageName());

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

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

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

I had tried the above with no success (App kept restarting) until I removed:

intent.setPackage(this.getPackageName());

which I'd recently added to see if I could fix notification issues with Android 14. Now I'm using:

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

to return to a timer that's running, and it is happily still running as desired.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文