Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the max size of a BigTextStyle notification

I have a messaging app integrated with Android Wear. Similar to Hangouts, when selecting a notification in an Android Wear Smartwatch, you can swipe to the second card which displays the conversation corresponding to the selected message. I implement it with a BigTextStyle notification, but I need to know the max number of chars BigTextStyle supports so I can trim properly the conversation when it's too large to completely fit. I couldn't find this info on documentation.

After some investigation, max chars are about 5000, at least in an Android Wear emulator. Therefore, I can do something like:

// scroll to the bottom of the notification card
NotificationCompat.WearableExtender extender = new NotificationCompat.WearableExtender().setStartScrollBottom(true);

// get conversation messages in a big single text
CharSequence text = getConversationText();

// trim text to its last 5000 chars
int start = Math.max(0, text.length() - 5000);
text = text.subSequence(start, text.length());

// set text into the big text style
NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle().bigText(text);

// build notification
Notification notification = new NotificationCompat.Builder(context).setStyle(style).extend(extender).build();

Does anyone know the exact number of characters that fit into a BigTextStyle notification? Does it changes between different devices?

like image 450
mbarrben Avatar asked Nov 25 '14 11:11

mbarrben


People also ask

How do I get big picture notifications on Android?

BigPictureStyle is used for showing a big picture in the notification item. It has two states — collapsed state and expanded state. In collapsed state, it shows exactly the same as an ordinary notification.

What is BigTextStyle?

Notification.BigTextStyle. bigText(CharSequence cs) Provide the longer text to be displayed in the big form of the template in place of the content text.

How do I make my notification icons smaller?

Set the small icon resource, which will be used to represent the notification in the status bar. Set the small icon, which will be used to represent the notification in the status bar and content view (unless overridden there by a large icon ).


1 Answers

Short Answer The limit is 5120 characters (5KB) but you don't need to limit your messages. This is done for you on the builder.

Detailed Answer

On your code you are using NotificationCompat.BigTextStyle that internally uses NotificationCompat.Builder.

This is what happens When you call to setBigContentTitle

    /**
     * Overrides ContentTitle in the big form of the template.
     * This defaults to the value passed to setContentTitle().
     */
    public BigTextStyle setBigContentTitle(CharSequence title) {
        mBigContentTitle = Builder.limitCharSequenceLength(title);
        return this;
    }

The function limitCharSequenceLength do this

    protected static CharSequence limitCharSequenceLength(CharSequence cs) {
        if (cs == null) return cs;
        if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
            cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
        }
        return cs;
    }

And if we inspect the constant declaration we found this

    /**
     * Maximum length of CharSequences accepted by Builder and friends.
     *
     * <p>
     * Avoids spamming the system with overly large strings such as full e-mails.
     */
    private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
like image 192
Juan M. Rivero Avatar answered Oct 05 '22 20:10

Juan M. Rivero