Why doesn't Jelly Bean show the second row in a Notification?
Asked Answered
H

3

8

I'm currently looking into the NotificationCompat features of the Android Support Package v4 Rev 10. The documentation says that 'setContentText()' shows the second row in a notification. This is true for API 8 up to API 15. However, if I try to use this method in API 16 my Notifications will miss the second line. I see only the Title but not the second line. Adding multiple lines is no problem (using 'addline()').

Here's my code for the NotificationCompat.Builder I used:

private NotificationCompat.Builder buildNormal(CharSequence pTitle) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            getSherlockActivity());

    builder.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL);
    // set the shown date
    builder.setWhen(System.currentTimeMillis());
    // the title of the notification
    builder.setContentTitle(pTitle);
    // set the text for pre API 16 devices
    builder.setContentText(pTitle);
    // set the action for clicking the notification
    builder.setContentIntent(buildPendingIntent(Settings.ACTION_SECURITY_SETTINGS));
    // set the notifications icon
    builder.setSmallIcon(android.R.drawable.stat_sys_download_done);
    // set the small ticker text which runs in the tray for a few seconds
    builder.setTicker("This is your ticker text.");
    // set the priority for API 16 devices
    builder.setPriority(Notification.PRIORITY_DEFAULT);
    return builder;
}

And if I want to add multiple lines and show the notification, I uses this code:

NotificationCompat.Builder normal = buildNormal("This is an Expanded Layout Notification.");
    NotificationCompat.InboxStyle big = new NotificationCompat.InboxStyle(
            normal);

    // summary is below the action
    big.setSummaryText("this is the summary text");
    // Lines are above the action and below the title
    big.addLine("This is the first line").addLine("The second line")
            .addLine("The third line").addLine("The fourth line");

    NotificationManager manager = getNotificationManager(normal);
    manager.notify(Constants.NOTIFY_ID, big.build());

Is this a wanted feature of Jelly Bean, that setContentText is ignored or am I missing something? The code runs on all versions without errors, but I would like to add the second line with the same code I use on ICS or earlier.

I've also added two screenshots. the first from my ICS 4.0.3 Huawei MediaPad and the second from a Galaxy Nexus with 4.1.1. The second line from 1 is for simplicity the same String as the notification title. It is not visible on 2.

Thanks for your help in advance!

The ICS 4.0.3 device The JellyBean 4.1.1 device

Haldas answered 16/8, 2012 at 13:19 Comment(0)
S
7

Is this a wanted feature of Jelly Bean, that setContentText is ignored or am I missing something?

The value of setContextText() should be visible in the collapsed state (e.g., two-finger swipe up if expanded, or have it not be the top-most Notification). It will be replaced by NotificationCompat.InboxStyle in the expanded state, given your code above.

Semiconscious answered 16/8, 2012 at 14:5 Comment(5)
Thanks, I didn't know this gesture for notifications. But there is no way to show the second line without this gesture?Haldas
@MarkusRudel: "I didn't know this gesture for notifications" -- yeah, neither did I, before somebody pointed it out to me. "But there is no way to show the second line without this gesture?" -- use the same text in your InboxStyle setup somewhere (e.g., setSummaryText()).Semiconscious
After reading the design guidelines, I finally understood the part about the notification expand/collapse gestures... you can even open and close with the pinch gesture.Haldas
I got this message, any idea? NotificationCOmpat is correctly added to preject with compatibility lib v4 and works but: NotificationCompat.Style cannot be resolved to a typeAmplification
@Waza_Be: Try upgrading to a new version of the Android Support package JAR.Semiconscious
U
1

If you want to show the notification by default expanded state.Simply set PRIORITY_MAX of builder. Like as: builder.setPriority(Notification.PRIORITY_MAX);

Unimpeachable answered 4/4, 2016 at 4:31 Comment(0)
H
0

I've fixed this problem now with @CommonsWare's help and created a simple method, that will check the current API Level and decides what command should be used:

private void createCompatibleSecondLine(CharSequence pTitle,
        NotificationCompat.Builder pBuilder, InboxStyle pInboxStyle) {
    // set the text for pre API 16 devices (or for expanded)
    if (android.os.Build.VERSION.SDK_INT < 16) {
        pBuilder.setContentText(pTitle);
    } else {
        pInboxStyle.setSummaryText(pTitle);
    }
}

So it's nothing wild and far from perfect but it does the job for me. Improvements are always welcome :)

Haldas answered 17/8, 2012 at 13:44 Comment(4)
Since the calls are cheap, I'd just call both setContentText() and setSummaryText().Semiconscious
Ok, if both calls are equally cheap the method may be useless then. Will the call of the method and the additional if clause cost much more time to execute than just the call of both methods?Haldas
Calling both methods will be incrementally more expensive, but on the order of microseconds. And you aren't doing this in a loop, so it will really be microseconds, not microseconds * number-of-iterations. For tiny savings like this, I would go with the more maintainable approach of calling both methods.Semiconscious
@MarkusRudel I have adopted you code for my inbox style notification. But when I test in my Samsung android (4.2) handset it shows me in collapse state and I can see additional lines only on zoom gesture. When I test this in my Sony android (4.2) handset by default it shows me in expanded format. Any idea why it is happening? I want by default expanded form.Rachellerachis

© 2022 - 2024 — McMap. All rights reserved.