Android 如何在状态栏中显示通知文本?

Android 如何在状态栏中显示通知文本?,android,android-notifications,Android,Android Notifications,我想在状态栏中显示通知文本,如下图所示: 但我得到的是以下信息: 所以,正如您所看到的,我不能在状态栏中显示文本,只能显示图标。我的代码如下所示: Notification notification = new Notification.Builder(this) .setSmallIcon(R.drawable.icon) .setContentTitle("My app") .setTicker("Alex: hey

我想在状态栏中显示通知文本,如下图所示:

但我得到的是以下信息:

所以,正如您所看到的,我不能在状态栏中显示文本,只能显示图标。我的代码如下所示:

Notification notification = new Notification.Builder(this)
            .setSmallIcon(R.drawable.icon)
            .setContentTitle("My app")
            .setTicker("Alex: hey, want to grab lunch?")
            .setWhen(System.currentTimeMillis())
            .setContentText("Alex: hey, want to grab lunch?")
            .setPriority(Notification.PRIORITY_MAX)
            .build();

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification);

我该如何纠正呢?

这完全取决于应用程序运行的Android版本。如果是棒棒糖(5.0)或更高版本,则不会,文本不会出现在状态栏中。(粗体):

概述此辅助功能服务通知的文本从L版本开始,此文本不再显示在屏幕上,但它对辅助功能服务仍然有用(在该服务中,它作为通知出现的声音通知)

因此,它仍然值得拥有,因为它将向任何在手机上使用文本到语音无障碍服务的人宣读。它不会像你在问题中期望的那样起作用

或者,如果您希望通知简短地显示为,您可以将通知的优先级设置为(最高API 26):

如果不起作用,您可能还需要使用而不是
Notification.Builder

Notification notification = new Notification.Builder(this)
    ...
    .setPriority(Notification.PRIORITY_HIGH)
    .build();