4

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);
    }
Pavel Petrashov
  • 205
  • 2
  • 10
  • ID_NITIF should be a static field and not final. "An identifier for this notification unique within your application." – mach Dec 12 '14 at 11:29
  • I think you should try using `FLAG_ONE_SHOT`. `FLAG_UPDATE_CURRENT` overwrites data. [see this](https://androidhub.wordpress.com/tag/flag_update_current/) – MysticMagicϡ Dec 12 '14 at 11:32
  • I found a solution. for a week, no one could help. to the data stored in each notification you want to add action in intent intent.setAction("some"); – Pavel Petrashov Dec 12 '14 at 11:34

1 Answers1

11

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);
Christian Göllner
  • 5,808
  • 4
  • 19
  • 20
  • Why do you recommend mixing the timestamp with a String? Isn't the timestamp already unique and the ".hashCode()" returns the timestamp as an int? Thank you for any insights. – AJW Mar 27 '21 at 01:58
  • Also, if I using the PendingIntent to send a message to a BroadcastReceiver, do I need to get the putExtra("randomRequestCode") with Bundle extras = intent.getExtras() and extras.getInt("key")? Or I don't need to get the Extra it is just for the PendingIntent to trick the OS into thinking it is unique so a unique PendingIntent is created every time? – AJW Mar 27 '21 at 02:03