How to extend InboxStyle notification with only one line added?

I create my notification as follows:

NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); inboxStyle.setBigContentTitle(title); for (int position = 0; position < onlineCounter; position++) { inboxStyle.addLine(onlineName.get(position) + " is online now"); } NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext()); notificationBuilder.setStyle(inboxStyle); notificationBuilder.setContentTitle(title); notificationBuilder.setContentText(contentText); notificationBuilder.setNumber(cursor.getCount()); notificationBuilder.setSmallIcon(R.drawable.ic_stat_notify); notificationBuilder.setColor(getResources().getColor(R.color.notification_color)); notificationBuilder.setLargeIcon(icon); notificationBuilder.setContentIntent(launchIntent); notificationBuilder.setDeleteIntent(clearIntent); notificationBuilder.setDefaults(property); notificationBuilder.setAutoCancel(true); 

When two or more lines are added to inboxStyle, the notification expands and automatically displays all the attached lines when you open the notification box.

2 lines appended

But when only one line is added, the notification does not expand, and the line is not displayed. How can I make a line automatically visible?

1 line appended

+5
source share
1 answer

TL DR: In the current Lollipop implementation, there is a bug in the notification extension to show InboxStyle when there is only one line added without summary text. By calling setSummaryText on InboxStyle or adding another line, you can get around the error.

Please note that this is only an error when the notification box is fully open. Notifications without summary text and one line are expanded from the lock screen.

Full answer:

For each notification, there are style elements that change in the expanded state and unexpanded state. When you set the style using InboxStyle , you set the extended notification state.

So, there are two different topics for discussion: extended content text and unextended content text. The problem you are facing is that without setting the summary text field for the extended notification style, you cannot open access to expand the notification with only one line added for InboxStyle , so the system simply displays the text of the unextended content, and never expanded InboxStyle (For text with unexpanded content, you can simply indicate the number of users on the Internet and specify a title for the name of your application, but this is the solution for you.)

So, it’s inaccurate to say that “setContentText overwrites the contents of InboxStyle when only one row is added.” The reality is that the InboxStyle style or the extended notification style is not displayed at all.

Below I have included some sample code that should make the difference between extended and unextended states clear:

 private void generateNotification(ArrayList<String> onlineNames) { … // Every notification must have a small icon, a content title, and content text notificationBuilder.setSmallIcon(R.drawable.icon); notificationBuilder.setContentTitle("Unexpanded Content Title from Your Application"); notificationBuilder.setContentText(getUnexpandedContentText(onlineNames.size())); notificationBuilder.setNumber(onlineNames.size()); notificationBuilder.setColor(getResources().getColor(R.color.accent_color)); notificationBuilder.setDefaults(Notification.DEFAULT_ALL); notificationBuilder.setStyle(getExpandedNotificationStyle); // Add anything else after this and notify the system … } private Style getExpandedNotificationStyle(ArrayList<String> names) { NotificationCompat.InboxStyle expandedNotificationStyle = new NotificationCompat.InboxStyle(); expandedNotificationStyle.setBigContentTitle("Expanded Content Title"); // There seems to be a bug in the notification display system where, unless you set // summary text, single line expanded inbox state will not expand when the notif // drawer is fully pulled down. However, it still works in the lock-screen. expandedNotificationStyle.setSummaryText("Expanded notification summary Text"); for (String name : names) { expandedNotificationStyle.addLine(name + " is online now"); } return expandedNotificationStyle; } private String getUnexpandedContentText(int numOnlineFriends) { switch (numOnlineFriends) { case 0: return "No friends are online"; case 1: return "1 friend is online"; default: return numOnlineFriends + " friends are online"; } } 
+14
source

Source: https://habr.com/ru/post/1210475/


All Articles