Reputation: 23
I am developing an Android app. It receives a notification A, and shows content (related to A) if user clicks on the notification. The problems is: if user clicks on one of the old notifications when receiving several, it shows the content related to the newest notification, rather than itself. For example, after I received A, B, C, D (A is the newest one) notifications, if I click on notification A, it shows content A, but if I click on B(or C or D), it still shows content A.
The code I used to push notifications is as follow:
Intent intent = new Intent(this, MainActivity.class);// start the main activity
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(MainActivity.EXTRA_TITLE, title);
intent.putExtra(MainActivity.EXTRA_URL, url);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setWhen(System.currentTimeMillis())
.setTicker(title)
.setContentTitle(title)
.setContentText(beacon.getDetail())
.setAutoCancel(true)
.setOngoing(false) // if it is true, the user cannot dismiss it
.setSmallIcon(R.drawable.ic_launcher)
.setOnlyAlertOnce(true)
.setDefaults(Notification.DEFAULT_ALL)
.setLights(0xff00ff00, 1000, 300)
.setSound(Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "6"))
.setVibrate(new long[] {
0, 100, 200, 300
})
//.addAction(0,"Cancel", dismissIntent)
//.addAction(0,"Open App", pi)
.build();
notification.contentIntent = pi;
notificationManager.notify(id, notification);
I am not sure if this is the normal case on Android. If it is not, can anyone help me to fix it? If it is the normal case, can anyone suggest me a solution to implement the above requirement? Thank you in advance.
EDIT: thank you so much, guys. With your help, I figured out the problem. According to the PendingIntent doc, if two intents are the same except their "extra"s, and their associated pending intents are also the same, then the "two" pendingintents are actually just one pendingintent because the old pendingintent is reused when creating the new pendingintent. To avaid this (to realize my requirement), I need to make sure the two intents are not the same by modifying their action, data, type, class, and categories, or make sure the two pendingintents are not the same by modifying their request code. In my case, I chose the second solution:
PendingIntent pi = PendingIntent.getActivity(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
I assigned the id of notifications to the request code of their corresponding pendingintents. Even though intents are still sythe same (filterEquals), but their corresponding pendingintents are different for they have different request codes. The ids of different notifications are not the same for I want to show different content.
By experiment, it works correctly.
Upvotes: 2
Views: 395
Reputation: 5166
From PendingIntent docs:
It is important to know when two Intents are considered to be the same for purposes of retrieving a PendingIntent. A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by Intent.filterEquals. If you use two Intent objects that are equivalent as per Intent.filterEquals, then you will get the same PendingIntent for both of them.
There are two typical ways to deal with this.
If you truly need multiple distinct PendingIntent objects active at the same time (such as to use as two notifications that are both shown at the same time), then you will need to ensure there is something that is different about them to associate them with different PendingIntents. This may be any of the Intent attributes considered by Intent.filterEquals, or different request code integers supplied to getActivity(Context, int, Intent, int), getActivities(Context, int, Intent[], int), getBroadcast(Context, int, Intent, int), or getService(Context, int, Intent, int).
Upvotes: 2
Reputation: 1248
Try this and it will replace older notifications
notificationManager.notify(id, notification); <---make id the same integer for all notifications
Upvotes: 0