我想在状态栏中显示通知文本,如下图所示:
但我得到的是:
因此,如您所见,我无法在状态栏中显示文本,只能显示图标。我的代码如下所示:
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 版本。如果是 Lollipop (5.0) 或更高版本,则不会,文本不会出现在状态栏中。 From the documentation (我的粗体):
Text that summarizes this notification for accessibility services. As of the L release, this text is no longer shown on screen, but it is still useful to accessibility services (where it serves as an audible announcement of the notification's appearance).
所以它仍然值得拥有,因为它会读给在手机上使用文本到语音辅助功能服务的任何人。它只是不会按照您在问题中期望的方式工作。
或者,如果您希望您的通知短暂显示为 heads-up notification , 您可以将通知的优先级设置为 PRIORITY_HIGH
(最高 API 26):
Notification notification = new Notification.Builder(this)
...
.setPriority(Notification.PRIORITY_HIGH)
.build();
您可能还需要使用 NotificationCompat.Builder
而不是 Notification.Builder
如果这不起作用。
关于android - 如何在状态栏中显示通知文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49135643/