Reputation: 585
I build simple app with used GCM and notification, I already success implement that, but I have question about notification, in my case:
I got more than one notification, Example
Notif_1 -> Title : test_1 , Message : test_message_1
Notif_2 -> Title : test_2 , Message : test_message_2
Notif_3 -> Title : test_3 , Message : test_message_3
Notif_4 -> Title : test_4 , Message : test_message_4
The problem I just always got last notification bundle when I tap notification.
So when I tap notif_1
, I got bundle from notif_4
when I tap notif_2
, I got bundle from notif_4
What I want is when I tap notif_1
, must have bundle from notif_1
not from other notif
how to make like that?
private void sendNotification(String title, String msg) {
mNotificationManager =
(NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
Intent resultIntent = new Intent(ctx, BuyLevel.class);
Bundle bundle = new Bundle();
bundle.putString("title", title);
bundle.putString("message", msg);
resultIntent.putExtras(bundle);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(ctx);
stackBuilder.addParentStack(BuyLevel.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx);
mBuilder.setSmallIcon(R.drawable.icon_mini);
mBuilder.setContentTitle(title);
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(msg));
mBuilder.setContentText(msg);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setAutoCancel(true);
Notification notification = mBuilder.build();
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
int initialNotification = CommonUtilities.msgId.incrementAndGet();
mNotificationManager.notify(initialNotification, notification);
}
Upvotes: 0
Views: 97
Reputation: 159
According to the documentation of PendingIntent:
If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid.
[...]
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.
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).
So your code when requesting the PendingIntent should be something like:
int m=0;
private void sendNotification(String title, String msg) {
mNotificationManager = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
[...]
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(m++,
PendingIntent.FLAG_UPDATE_CURRENT);
[...]
}
Upvotes: 0
Reputation: 1870
Flag must be required with unique values. So we can set flag as random number. Change this line:
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
to
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, m);
Upvotes: 1