I send different notifications. But when you click on any one of them I get the data that have been sent to the last notification. how to fix it? I need to notice every store their data, and a new notice does not replace them.I use a flag PendingIntent.FLAG_UPDATE_CURRENT
but I use the left all the offers of flags too.
private void generateNotification(Context context, String title, String message,int groupid,Intent data) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
Intent intent = new Intent(context,MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
if (groupid==1){
intent.putExtra("guest",data.getStringExtra("guest"));
intent.putExtra("hotel",data.getStringExtra("hotel"));
intent.putExtra("room",data.getStringExtra("room"));
}
if (groupid==5){
intent.putExtra("hotel",data.getStringExtra("hotel"));
}
if (groupid==4){
intent.putExtra("hotel",data.getStringExtra("hotel"));
intent.putExtra("guest",data.getStringExtra("guest"));
}
intent.putExtra("group_id",groupid);
Log.d("mylogout","group_id: "+groupid);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_stat_gcm)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_gcm))
.setTicker("Новое сообщение")
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle(title)
.setContentText(message)
.build();
notificationManager.notify(ID_NITIF++, notification);
}
The trick is to add a different requestCode
on the PendingIntent for each different notification.
Documentation is:
public static PendingIntent getActivity (Context context, int requestCode, Intent intent, int flags)
The system uses it to compare PendingIntents, and if you pass the same code to different requests, it thinks its the same, and won't update it. To fix it, add a different requestCode for each different notification.
What I ussually do to make sure ALL notifications are well updated:
//Use the hashcode of current timestamp mixed with some string to make it unique
int requestCode = ("someString" + System.currentTimeMillis()).hashCode();
//Also add it to the intent, to make sure system sees it as different/modified
intent.putExtra("randomRequestCode", requestCode);
//Add the requestCode to the PendingIntent
PendingIntent pendingIntent = PendingIntent.getActivity(context, requestCode , intent, PendingIntent.FLAG_UPDATE_CURRENT);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With