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