Reputation: 3503
Most of you who are familiar with the Notification and PendingIntent APIs will know that setLatestEventInfo is deprecated now.
I am therefore trying to replace my existing code (depending on the deprecated method):
Context context = getApplicationContext();
Intent activityIntent = new Intent(context, Activity.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Notification notification = new Notification(R.drawable.icon, getString(R.string.notify),System.currentTimeMillis());
PendingIntent startIntent = PendingIntent.getActivity(context, 0, activityIntent, 0);
notification.setLatestEventInfo(context, getString(R.string.notify), getString(R.string.notifysummary), startIntent);
this.startForeground(1234,notification);
As you may guess, I am calling this from inside a service that runs in the background. When the service starts, it will bring up the notification. This is a persistent, ongoing notification that brings the activity "Activity.class" to front in case it exists and creates a new instance of the activity in case it was killed meanwhile. Works fine without any problems.
Now, wanting to migrate to the newer API level, I am trying to replace the above code with the following NotificationBuilder example:
Context context = getApplicationContext();
Intent activityIntent = new Intent(context, Activity.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent startIntent = PendingIntent.getActivity(context, 0, activityIntent, Intent.FLAG_ACTIVITY_SINGLE_TOP);
Notification notification = new Notification.Builder(context).setSmallIcon(R.drawable.ic_launcher).setContentText("App running").setContentTitle("My app").setOngoing(true).setAutoCancel(false).setContentIntent(startIntent).build();
this.startForeground(1234,notification);
But clicking on the Notification does not have any effect. However, I already tried "Intent.FLAG_ACTIVITY_NEW_TASK" because documentation says I should. This creates a new task even if the activity is already on top, in constrast to what the doc says: PendingIntent, FLAG_ACTIVITY_NEW_TASK.
Did anybody encounter the same problem? How to construct a persistent notification that is NOT dismissed when clicked and brings the activity to top if somewhere on the stack, else creates a new instance of it?
Thanks for your help.
Upvotes: 1
Views: 2228
Reputation: 95578
This is not correct:
PendingIntent startIntent = PendingIntent.getActivity(context, 0, activityIntent, Intent.FLAG_ACTIVITY_SINGLE_TOP);
You are passing an Intent
flag to getActivity()
. The 4th parameter for getActivity()
takes PendingIntent
flags, such as:
You are passing Intent.FLAG_ACTIVITY_SINGLE_TOP
as the 4th parameter. That constance actually has the value 0x20000000. Coincidentally, PendingIntent.NO_CREATE
also has the value 0x20000000, so what you are doing is the same as this:
PendingIntent startIntent = PendingIntent.getActivity(context, 0, activityIntent, PendingIntent.NO_CREATE);
That's your problem.
Just set the 4th parameter to getActivity()
to 0
, like in your original code.
Upvotes: 2