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