What is the maximum notification size for BigTextStyle

I have a messaging app integrated with Android Wear . Like Hangouts , when you select a notification in the Android Wear Smartwatch, you can scroll a second card that displays the conversation corresponding to the selected message. I implement it with a BigTextStyle notification, but I need to know the maximum number of BigTextStyle characters, so I can correctly crop the conversation when it is too large to fit completely. I could not find this information in the documentation.

After some research, the maximum characters are around 5000, at least in the Android Wear emulator. So 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 change between different devices?

+5
source share
1 answer

Short answer The limit is 5120 characters (5 KB), but you do not need to limit your messages. This is done for you on the builder.

Detailed answer

In your code, you are using NotificationCompat.BigTextStyle , which uses NotificationCompat.Builder .

This happens when you call 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 limitCharSequenceLength function does 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 check the declaration of the constant, we find it

  /** * 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; 
+8
source

Source: https://habr.com/ru/post/1207660/


All Articles