What's the max size of a BigTextStyle notification

2.9k views Asked by At

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?

1

There are 1 answers

2
Juan M. Rivero On BEST ANSWER

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;