Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notifications will be overwritten each other

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);
    }
like image 503
Pavel Petrashov Avatar asked Dec 19 '22 08:12

Pavel Petrashov


1 Answers

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);
like image 115
Christian Göllner Avatar answered Feb 01 '23 09:02

Christian Göllner