22

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);
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
ozmike
  • 2,738
  • 1
  • 33
  • 40

6 Answers6

33

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.

Community
  • 1
  • 1
ozmike
  • 2,738
  • 1
  • 33
  • 40
13

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);
Johann
  • 27,536
  • 39
  • 165
  • 279
5

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);
Waza_Be
  • 39,407
  • 49
  • 186
  • 260
  • 2
    Hi thanks for answering I put this in manifest, fixed the problem for me android:launchMode="singleInstance" also these answers allude to other things of interest.. http://stackoverflow.com/questions/4047683/android-how-to-resume-an-app-from-a-notification and http://stackoverflow.com/questions/4047683/android-how-to-resume-an-app-from-a-notification – ozmike Apr 10 '11 at 12:44
1

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
Tobliug
  • 2,992
  • 30
  • 28
0

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

Harol
  • 93
  • 1
  • 4
0

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

B.Ondrej
  • 381
  • 4
  • 14