Android:无法在通知中显示多行文本

5

我想使用BigTextStyle在通知中显示多行文本,但无法实现。我正在使用以下代码:

public void sendNotification(View view) {
    String msgText = "Jeally Bean Notification example!! "
            + "where you will see three different kind of notification. "
            + "you can even put the very long string here.";

    NotificationManager notificationManager = getNotificationManager();
    PendingIntent pi = getPendingIntent();
    android.app.Notification.Builder builder = new Notification.Builder(
            this);
    builder.setContentTitle("Big text Notofication")
            .setContentText("Big text Notification")
            .setSmallIcon(R.drawable.ic_launcher).setAutoCancel(true)
            .setPriority(Notification.PRIORITY_HIGH)
            .addAction(R.drawable.ic_launcher, "show activity", pi);
    Notification notification = new Notification.BigTextStyle(builder)
            .bigText(msgText).build();

    notificationManager.notify(0, notification);
}

public NotificationManager getNotificationManager() {
    return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}

public PendingIntent getPendingIntent() {
    return PendingIntent.getActivity(this, 0, new Intent(this,
            MainActivity.class), 0);
}

我甚至无法在通知中看到'msgText'。有什么想法吗? 谢谢您的帮助。
4个回答

4

只需将通知样式设置为BigText

   NotificationCompat.BigTextStyle bigStyle =
   new NotificationCompat.BigTextStyle();
   bigStyle.setBigContentTitle(title);
   bigStyle.bigText(messageBody);
   builder.setStyle(bigStyle);

4

问题已解决!

代码没有问题,只是通知栏显示的空间不够大。当我拔掉数据线时,它以期望的方式显示出来了。 :-)

感谢所有尝试帮助我的人。


有没有办法默认以完全展开模式显示通知,而不是通过捏合扩展。谢谢。 - Anand Prakash
你的代码对我不起作用。你在这方面做了任何更改吗?它只会显示一行文本,去掉尾随文本。 - keen
@AnandPrakash ,如答案所述,只需断开电缆即可。 - Rumit Patel

0

尝试将内容意图值设置为相应的值。

Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle("Big text Notification")
        .setContentText("Big text Notification")
        .setSmallIcon(R.drawable.ic_launcher)
        .setAutoCancel(true)

        .setContentIntent(pi) // <- this new line

        .setPriority(Notification.PRIORITY_HIGH)
        .addAction(R.drawable.ic_launcher, "show activity", pi);

-5

.setPriority(Notification.PRIORITY_HIGH) - 这行代码解决了我的问题。多行保持大小。


优先级与此无关。相对于此通知的优先级。优先级是用户应该花费多少宝贵的注意力来处理此通知的指示。在某些情况下,低优先级通知可能会被隐藏,而用户可能会因高优先级通知而受到干扰。系统将在呈现通知时确定如何解释此优先级。 - Matin Petrulak
你不应该仅仅为了修复一些错误而使用优先级。优先级是为了优先处理重要任务。请阅读文档。 - Lukas Liesis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接