繁体   English   中英

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

[英]How to show Notification text in status bar?

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

在此处输入图片说明

但相反,我得到以下信息:

在此处输入图片说明

因此,如您所见,我无法在状态栏中显示文本,只能显示图标。 我的代码如下所示:

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) 或更高版本,则不会,文本不会出现在状态栏中。 从文档(我的粗体):

总结此无障碍服务通知的文本。 从 L 版本开始,此文本不再显示在屏幕上,但它对无障碍服务仍然有用(它作为通知出现的声音公告)。

所以它仍然值得拥有,因为它会被任何在手机上使用文本到语音辅助功能服务的人读出。 它只是不会以您在问题中期望的方式工作。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM