推送通知内容文本未显示在 Lollipop 上

Push Notification content text not showing on Lollipop

这是显示推送通知的代码:

// 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());

这在更高的 android 版本上工作正常,但通知文本不会显示 Lollipop 版本。这是 Lollipop 设备上的屏幕截图:

我调试验证了这里传递的是文本。请建议。提前致谢。

来自官方doc :

设置群组摘要

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

文本不显示,因为显示的是groupBuilder,它不包含任何文本,所以对于API 27的较小版本,将您的文本添加到groupBuilder或创建一个样式来总结其他通知的内容例如:

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("janedoe@example.com"));

更新: 在 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());
    }

}

代码工作原理概述: