35

I need to implement expand and collapse notification in status bar for android 4.0 and above version. I have search on google for this but didn't getting any code solution for implementation does anybody have I idea how to implement this

Thank You in advance

Pratik
  • 30,639
  • 18
  • 84
  • 159

6 Answers6

49

An expandable Notification is a special case of a Notification Big View. If the Big View is not at the top of the notification drawer, it it shown 'closed' and is expandable by swipe. Quote from Android Developers:

A notification's big view appears only when the notification is expanded, which happens when the notification is at the top of the notification drawer, or when the user expands the notification with a gesture. Expanded notifications are available starting with Android 4.1.

The Big View Notification can be created as follows:

Notification notification = new Notification.BigTextStyle(builder)
.bigText(myText).build();

or

Notification notification = new Notification.BigPictureStyle(builder)
.bigPicture(
  BitmapFactory.decodeResource(getResources(),
    R.drawable.my_picture)).build();

Here is a tutorial.

Gunnar Karlsson
  • 28,350
  • 10
  • 68
  • 71
  • can we create expandable notification in android 4.0 versions ? – prateek Oct 24 '13 at 06:04
  • @prateek Android 4.1 : http://developer.android.com/guide/topics/ui/notifiers/notifications.html#BigNotify – Gunnar Karlsson Oct 24 '13 at 10:26
  • this means there is no way to increase notification height before android 4.1 even for custom notification ? – prateek Oct 24 '13 at 12:21
  • I implemented it the same way using BigView, i have one problem, on some phone sliding the notification to expand is bit difficult. We have to use the full finger to roll over the notification area(full surface area of finger over notification and then slide down). Do any off the application use bigView, so that i can compare if it is device dependent or there is some code error. – Sagar Patil Apr 11 '16 at 11:21
  • http://stackoverflow.com/questions/42527534/pending-action-expand-notification-on-click Please is it possible ! – Nikola Lukic Mar 01 '17 at 12:51
43
Notification noti = new Notification.Builder()
... // The same notification properties as the others
.setStyle(new Notification.BigPictureStyle().bigPicture(mBitmap))
.build();

You change

.setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert))

along with the announcement OK !!!

notification = new NotificationCompat.Builder(context)

Here is an example:

Expandable Notifications and Notifications Groups

You can set Code

Intent intent = new Intent(context, ReserveStatusActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
intent = new Intent(String.valueOf(PushActivity.class));
intent.putExtra("message", MESSAGE);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(PushActivity.class);
stackBuilder.addNextIntent(intent);
// PendingIntent pendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

// android.support.v4.app.NotificationCompat.BigTextStyle bigStyle = new     NotificationCompat.BigTextStyle();
// bigStyle.bigText((CharSequence) context);

notification = new NotificationCompat.Builder(context)
    .setSmallIcon(R.mipmap.ic_launcher)
    .setContentTitle(th_title)
    .setContentText(th_alert)
    .setAutoCancel(true)
 // .setStyle(new Notification.BigTextStyle().bigText(th_alert)  ตัวเก่า
 // .setStyle(new NotificationCompat.BigTextStyle().bigText(th_title))
    .setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert))
    .setContentIntent(pendingIntent)
    .setNumber(++numMessages)
    .build();

notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationManager.notify(1000, notification);

or

private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data) {
            Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
    
            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                   // .setContentTitle(notification.getTitle())
                    .setContentTitle(getResources().getText(R.string.app_name))
                    .setContentText(notification.getBody())
                    .setAutoCancel(true)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setContentIntent(pendingIntent)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(notification.getBody()))
                    .setContentInfo(notification.getTitle())
                    .setLargeIcon(icon)
                    .setColor(Color.RED)
                    .setSmallIcon(R.drawable.logo);
    
            try {
                String picture_url = data.get("picture_url");
                if (picture_url != null && !"".equals(picture_url)) {
                    URL url = new URL(picture_url);
                    Bitmap bigPicture = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                    notificationBuilder.setStyle(
                            new NotificationCompat.BigPictureStyle().bigPicture(bigPicture).setSummaryText(notification.getBody())
                    );
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
            notificationBuilder.setLights(Color.YELLOW, 1000, 300);
    
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, notificationBuilder.build());
        }

27/07/2021 :

You should can read detail this : Expandable Notifications and Notifications Groups

Milan Tejani
  • 372
  • 5
  • 21
Pong Petrung
  • 1,437
  • 17
  • 14
4

I Couldn't able to set new instance of new NotificationCompat.BigTextStyle() in .setStyle() method of Notification. So I have used the below one, new instance of new Notification.BigTextStyle() in .setStyle().

      Notification builder =new Notification.Builder(this)
                    .setSmallIcon(Notification_icons[icon])
                    .setContentTitle(title)
                    .setContentText(description)
                    .setChannelId(channelID_Default)
                    .setOngoing(true)
                    .setStyle(new Notification.BigTextStyle()
                            .bigText(description))
                    .build();
anand krish
  • 4,281
  • 4
  • 44
  • 47
2

There is a method for this function you can show a small icon when notification collapsed and show a large one when a notification expanded.

val bitmap = BitmapFactory.decodeResource(resources, R.drawable.notification)

var notification = NotificationCompat.Builder(context, CHANNEL_ID)
    .setSmallIcon(R.drawable.new_post)
    .setContentTitle(imageTitle)
    .setContentText(imageDescription)
    .setLargeIcon(bitmap)
    .setStyle(NotificationCompat.BigPictureStyle()
            .bigPicture(bitmap)
            .bigLargeIcon(null))
    .build()
Nimisha V
  • 461
  • 4
  • 12
2

(2021) Stumbled upon this question when I was having issues with expanding my notification which had a larger text. Solved by referring to the Android docs on Expandable Notifications: Create an Expandable Notification

Nimisha V's answer shows how to expand a large image on a notification. The below code is used for expanding large text on a notification.

var notification = NotificationCompat.Builder(context, CHANNEL_ID)
    .setSmallIcon(R.drawable.icon)
    .setContentTitle(notification_title)
    .setContentText(notification_message)
    .setStyle(NotificationCompat.BigTextStyle()
            .bigText(notification_message))
    .build()
Shizuku
  • 147
  • 1
  • 9
0

We can't create expandable notification in below android 4.1 versions. But Instead of this we can do that we can stacked the notifications and then we can set a pending intent to our pretty custom activity which shows all notification in list. User will happy to see this :)

g4gaj
  • 85
  • 2
  • 11