I am trying to display multiple lines of Text using BigTextStyle in Notification but unable to do so. I am using the code below.

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);
}

I can't even see 'msgText' in the notification. Any idea why? Thanks for helping.

有帮助吗?

解决方案

Solved!

Code was fine, its just that there was not enough space for big notification. When I disconnected data cable, it got displayed in desired manner. :-)

Thanks to all who tried to help.

其他提示

Just set notification style to BigText

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

Try to set content intent value as well.

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);

.setPriority(Notification.PRIORITY_HIGH) - this line solved my problem. The multiline conserve size.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top