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

标签 android android-notifications

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

enter image description here

但我得到的是:

enter image description here

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

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/

相关文章:

android - 寻找适用于 Android 2.3 ARM 平台的 iptables 二进制文件?

android - 检测 "use only 2G networks"设置

android - 如何避免在 Android 上的 onCreate 中创建 View ?

android - Xamarin Android SetBackgroundDrawable 已弃用但未弃用 SetBackground()

Android:收到新通知时如何打开屏幕?

android - 显示默认展开的 android 通知操作按钮

java - Android Proximity Alert - 不触发

Flutter 本地通知 int id 参数

Android 本地通知有时不会显示

php - Android:无法使用php pdo在mysql中插入数据