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
To remove a persistent notification on Android as fast as possible, first, press-and-hold on it. Alternatively, swipe the notification left or right to reveal a gear icon on either side, and then tap on it. The notification expands. Tap on “Turn off notifications” at the bottom.
1 Tap Notification Settings on the notification panel or tap the Settings app. 2 Tap Notifications. 3 Tap App icon badges.
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.
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
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