4

I am in the process of teaching myself how to work with Phonegap notifications on Android. Whilst displaying a notification appears to be a fairly straightforward process

public void triggerTestNotification(String tag, int id,Context ctxt) 
{
  Intent notificationIntent = new Intent(context, PallActivity.class);
  notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
  Intent.FLAG_ACTIVITY_CLEAR_TOP);

  notificationIntent.setAction(Intent.ACTION_MAIN);
  notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);

  PendingIntent contentIntent = PendingIntent.getActivity(ctxt, 0,  
  notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

  Notification not = new Notification.Builder(ctxt)
  .setContentTitle("Title").setContentText("Title")
  .setTicker("Tick tock")
  .setAutoCancel(true) 
  .setContentIntent(contentIntent)
  .setSmallIcon(ctxt.getApplicationInfo().icon).build();
   NotificationManager notificationManager = (NotificationManager)    
   ctxt.getSystemService(Context.NOTIFICATION_SERVICE);
   notificationManager.notify(tag, id, not);
 }

what I have found rather more difficult is the following

  • The user launches the app
  • The user goes away and starts doing something else - the app is backgrounded
  • A notification arrives
  • Is displayed
  • The user clicks on the notification.

At this point the app should come back to the foreground. I thought my intent code

public class PallActivity extends Activity
{
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
   super.onCreate(savedInstanceState);
  finish();
  forceMainActivityReload();
 }

 private void forceMainActivityReload()
 {
  PackageManager pm = getPackageManager();
  Intent launchIntent =    
  pm.getLaunchIntentForPackage(getApplicationContext().getPackageName());           
  startActivity(launchIntent);
 }

 @Override
 protected void onResume() 
 {
  super.onResume();
  final NotificationManager notificationManager = (NotificationManager) 
  this.getSystemService(Context.NOTIFICATION_SERVICE);
  notificationManager.cancelAll();
 }

}

would deal with this but it does nothing at all. Clearly, I am getting something wrong here - perhaps in the Intent flags. I'd be much obliged to anyone who might be able to put me on the right track

DroidOS
  • 8,530
  • 16
  • 99
  • 171
  • why do you call `finish` in `onCreate` ? – Boris Strandjev May 10 '16 at 14:07
  • I placed the `finish()` there because I saw it in another plugin. However, I have tried my code with `finish()` moved to the bottom of `onCreate` as well as removed all together. The app still does not get back to the foreground. – DroidOS May 11 '16 at 07:06
  • 1
    And can you please say what is the purpose of the Pall activity - I mean it immediately redirects to the launcher activity of your app. Isn't `PallActivity` your launcher activity? Can you include your manifest? – Boris Strandjev May 11 '16 at 07:49
  • First of all use [NotificationCompat.Builder](http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html) and from your notificationIntent remove the following: notificationIntent.setAction(Intent.ACTION_MAIN); notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); – Ankit Aggarwal May 12 '16 at 17:59

1 Answers1

2

Remove finish(); from your oncreate, it will call finish before you proceed anywhere.

Instead of starting PallActivity, from which is seems you are intending to start a second activity or at least treating the PallActivity as a second activity. You don't need to kick start it like that from within forceMainActivityReload();. I would be starting the activity you wish to start from the notification intent, rather than making it more complex and confusing the issue.

notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;   
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

Ensure that the context you are using is:

Context context = getApplicationContext();

And, as mentioned in the comments remove these from the notification:

notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);

Here's a plethora of more detail if required:

Resume an activity when clicked on a notification

Android: How to resume an App from a Notification?

resuming an activity from a notification

Notification Resume Activity

Intent to resume a previously paused activity (Called from a Notification)

Android: resume app from previous position

How to make notification intent resume rather than making a new intent?

Community
  • 1
  • 1