Wamiq
Wamiq

Reputation: 1364

Push Notification content text not showing on Lollipop

Here is the code for showing push notification:

// receiverUid = unique uid for a receiver
// notificationUid = unique notification_uid
// receiverName, title, body are String variables.
NotificationCompat.Builder groupBuilder =
                new NotificationCompat.Builder(this, "NOTIFICATION_CHANNEL")
                        .setSmallIcon(R.drawable.ic_app_icon_24)
                        .setColor(ColorUtils.getColor(getApplicationContext(), R.color.blue_700))
                        .setGroupSummary(true)
                        .setGroup(receiverUid)
                        .setAutoCancel(true)
                        .setSubText(receiverName)
                        .setContentIntent(pendingIntentGroup);

        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this, "NOTIFICATION_CHANNEL")
                        .setSmallIcon(R.drawable.ic_app_icon_24)
                        .setColor(ColorUtils.getColor(getApplicationContext(), R.color.blue_700))
                        .setContentTitle(title)
                        .setContentText(body)
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                        .setContentIntent(pendingIntentNotification)
                        .setGroup(receiverUid)
                        .setSubText(receiverName)
                        .setAutoCancel(true);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

// group notification is based on receiverUid, so the notifications are grouped under different receipients
        notificationManager.notify(receiverUid, 0, groupBuilder.build());
        notificationManager.notify(receiverUid + "_" + notificationUid, 0, builder.build());

This works fine on higher android versions, but the notification text is not shown for Lollipop versions. Here is screenshot on a Lollipop device:

Notification text not showing on Lollipop

I debugged and verified that the text is passed here. Please suggest. Thanks in Advance.

Upvotes: 1

Views: 269

Answers (1)

Shay Kin
Shay Kin

Reputation: 2677

From official doc :

Set a group summary

On Android 7.0 (API level 24) and higher, the system automatically builds a summary for your group using snippets of text from each notification. The user can expand this notification to see each separate notification, as shown in figure 1. To support older versions, which cannot show a nested group of notifications, you must create an extra notification that acts as the summary. This appears as the only notification and the system hides all the others. So this summary should include a snippet from all the other notifications, which the user can tap to open your app

The text is not displayed because the groupBuilder is the one that is displayed and it does not contain any text so for the lesser version of API 27 add your text to the groupBuilder or create a style to summarize the contents of the other notification for example :

NotificationCompat.Builder groupBuilder =
                new NotificationCompat.Builder(this, "NOTIFICATION_CHANNEL")
                        .setSmallIcon(R.drawable.ic_app_icon_24)
                        .setColor(ColorUtils.getColor(getApplicationContext(), R.color.blue_700))
                        .setGroupSummary(true)
                        .setGroup(receiverUid)
                        .setAutoCancel(true)
                        .setSubText(receiverName)
                        .setContentIntent(pendingIntentGroup)
                        .setStyle(new NotificationCompat.InboxStyle()
                                 .addLine("Alex Faarborg  Check this out")
                                 .setBigContentTitle("2 new messages")
                                 .setSummaryText("[email protected]"));

Update : implement the notification count and the messages summary in the Style

public class MainActivity extends AppCompatActivity {

    private int notificationCount = 0;
    private NotificationCompat.InboxStyle style;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // also you can Save the Notification count on the sharePreference
        // for simulate the notification with multiple message
        String[][] person = {{"Bill", "Jones"}, {"Janet", "Kline"}, {"George", "Bailey"},
                {"Ellan", "Sanches"}, {"Tom", "Nguyen"}, {"William", "Walters"}, {"Author", "James"},
                {"Henry", "Daniels"}, {"Mike", "Franklin"}, {"Julie", "Andrews"}};

        // i have just testing with the TimeDownCount to add notifications every 20 seconds
        CountDownTimer countDownTimer = new CountDownTimer(2000, 10) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
                // get random person from the array person 
                Random random = new Random();
                int randomInt = random.nextInt(person.length - 1);
                // show notification
                showNotification(person[randomInt][0] + " " + person[randomInt][1]);
                if (notificationCount < 10) {
                    this.start();
                }
            }
        };
        countDownTimer.start();
        style = new NotificationCompat.InboxStyle();
    }


    private void showNotification(String message) {
        notificationCount++;
        
        style.addLine(message); // add person to the style 
        style.setBigContentTitle(notificationCount + " new messages"); // show Notification count in the Title

        NotificationCompat.Builder groupBuilder =
                new NotificationCompat.Builder(this, "NOTIFICATION_CHANNEL")
                        .setSmallIcon(R.drawable.ic_baseline_mic_24)
                        .setColor(Color.BLUE)
                        .setGroupSummary(true)
                        .setGroup("receiverUid")
                        .setAutoCancel(true)
                        .setStyle(style)
                        .setSubText("Your Action");

        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this, "NOTIFICATION_CHANNEL")
                        .setSmallIcon(R.drawable.ic_baseline_mic_24)
                        .setColor(Color.BLUE)
                        .setContentTitle("title")
                        .setContentText("body")
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                        .setGroup("receiverUid")
                        .setSubText("receiverName")
                        .setAutoCancel(true);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify("receiverUid", 0, groupBuilder.build());
        notificationManager.notify("receiverUid" + "_" + "notificationUid", 0, builder.build());
    }

}

overview of how the code works :

enter image description here

Upvotes: 1

Related Questions