Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update notification content of the existing notification present on Notification bar like Whatspp,Gmail,etc in Android

When I observed the whats app and gmail notifications,they are automatically updates the existing notification of there app without creating new one. So How I can achieve this? enter image description here

I have created One notification like in above image.So

  • When Notification create at that time There are four messages are there.

  • So When I get Fifth message it should append content of existing notification like shown in image with Red color.

Please suggest me which methods are used in Gmail like app?

How I can achieve this?

like image 797
Anil Ravsaheb Ghodake Avatar asked Oct 29 '22 21:10

Anil Ravsaheb Ghodake


1 Answers

My problem resolve with the below link

Update text of notification, not entire notification

Here is my code:-

        PendingIntent pendingIntent = PendingIntent.getBroadcast(objContext, (int) (Math.random() * 100), intent, PendingIntent.FLAG_CANCEL_CURRENT);
        NotificationManager manager = (NotificationManager) objContext.getSystemService(Context.NOTIFICATION_SERVICE);
        if (firstTime) {
            intCount++;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                inboxStyle = new Notification.InboxStyle();
            }
            Bitmap icon = BitmapFactory.decodeResource(objContext.getResources(), R.drawable.ic_launcher);
//            Notification.Builder builder = new Notification.Builder(objContext);
            builder = new Notification.Builder(objContext);
            builder.setAutoCancel(true);
            builder.setContentTitle("Studyboard");
            builder.setDefaults(Notification.DEFAULT_ALL);
            builder.setSmallIcon(R.drawable.ic_launcher_transparent_s);
            builder.setLargeIcon(icon);
            builder.setWhen(Calendar.getInstance().getTimeInMillis());
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                builder.setStyle(inboxStyle);
            }
            builder.setOngoing(false);
            builder.setContentIntent(pendingIntent);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                builder.build();

                inboxStyle.addLine(senderFullName + " : " + strDisplayMsg);
                inboxStyle.setSummaryText("");
            }
            System.out.println("Creating New Notification");
            Notification myNotication = builder.getNotification();
            manager.notify("Example", 12345, myNotication);
            firstTime = false;

        } else {
            intCount++;
            builder.setWhen(Calendar.getInstance().getTimeInMillis());
            System.out.println("Not Creating");
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                builder.setStyle(inboxStyle);
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                if (intCount <= 3)
                    inboxStyle.addLine(senderFullName + " : " + strDisplayMsg);
                if (intCount > 3 && intCount - 3 > 0)
                    inboxStyle.setSummaryText("+" + (intCount - 3) + " more messages");
            }
            Notification myNotication = builder.getNotification();
            manager.notify("Example", 12345, myNotication);
        }

And For next execution Reset the flag and count in Broadcast Receiver which catch action for the Notification

like image 93
Anil Ravsaheb Ghodake Avatar answered Nov 15 '22 07:11

Anil Ravsaheb Ghodake