How to get text of Stacked Notifications in Android

12,493

Solution 1

WhatsApp application has structure for sending notification like this :

        Case                                 Notification

Message comes from A : Hi                   Title : A    Text: Hi

Message comes from A : How are you          Title : A    Text: How are you

                                            Title : A    Text: 2 new messages


Message comes from B : Hello                Title : B    Text: Hello

                                            Title : B    Text: 1 new message

                                            Title : A    Text: 2 new messages

                     Title : WhatsApp  Text: 3 new messages from 2 conversation
---- Here comes the stacking ----

Message comes from C : Good work            Title : C    Text: Good work

                                            Title : C    Text: 1 new message

                                            Title : B    Text: 1 new message

                                            Title : A    Text: 2 new messages

                     Title : WhatsApp  Text: 4 new messages from 3 conversation


 ---- This way when new sender message comes, previoud notifications also comes and we get callback in NotificationListener ----

Last notification comes with Title as Package Name : WhatsApp and Text as : X messages from Y Conversation

To get Text :

sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TEXT).toString();

To get Title :

sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TITLE).toString();

To work with this sturcture of stacking, we need to parse this notification stack and display only selective information in our application

I hope my answer will help and solve your query

Solution 2

If you're working with Android 7.0+, WhatsApp uses MessageStyle Expanded Notifications. Here - https://developer.android.com/training/notify-user/expanded.html#message-style

To retrieve all 5 messages from a notification like

MyFriend (5 messages)
testt

Do this:

Bundle extras = mysbn.getNotification().extras;
if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)){
        Parcelable b[] = (Parcelable[]) extras.get(Notification.EXTRA_MESSAGES);

        if(b != null){
            content = "";
            for (Parcelable tmp : b){

                Bundle msgBundle = (Bundle) tmp;
                content = content + msgBundle.getString("text") + "\n";

                /*Set<String> io = msgBundle.keySet(); // To get the keys available for this bundle*/

            }
        }
    }

Same as my answer here.

Solution 3

I think this can help you:

CharSequence[] lines = 
extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES);
if(lines != null && lines.length > 0) {
   StringBuilder sb = new StringBuilder();
   for (CharSequence msg : lines)
      if (!TextUtils.isEmpty(msg)) {
         sb.append(msg.toString());
         sb.append('\n');
      }
   return sb.toString().trim();
}
CharSequence chars = 
extras.getCharSequence(Notification.EXTRA_BIG_TEXT);
if(!TextUtils.isEmpty(chars))
   return chars.toString();
Share:
12,493

Related videos on Youtube

user1406716
Author by

user1406716

Updated on June 04, 2022

Comments

  • user1406716
    user1406716 about 2 years

    The question is how to get the TEXT (not title) field of all incoming notifications when they get stacked (like in Whatsapp).

    enter image description here

    public class NLService extends NotificationListenerService {
    public void onNotificationPosted(StatusBarNotification sbn) {
    
        Log.v(Constants.TAG_notifs,
                "------------------------- in onNotificationPosted(), Notification Text = "
                        + sbn.getNotification().tickerText);
    
    
        Bundle extras = sbn.getNotification().extras;
    
        if (extras.containsKey("android.text")) {
            if (extras.getCharSequence("android.text") != null) {
                String text = extras.getCharSequence("android.text").toString();
                Log.v(Constants.TAG_notifs,
                        "------------------------- in onNotificationPosted(), Bundle.text != NULL, so here it is = "
                                + text);
            } 
        }
    
        if (extras.containsKey("android.title")) {
            Log.v(Constants.TAG_notifs,
                    "------------------------- in onNotificationPosted(), Bundle android.title = "
                            + extras.getString("android.title"));
        }
    
    }
    
    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        //super.onNotificationRemoved(sbn);
    }
    

    } The first time when a Whatsapp notification arrives from a single user this line (String text = extras.getCharSequence("android.text").toString();) is successfully able to read text, but after that when more messages come in and notifications get stacked (like in the picture shown above), the variable text is always NULL.

    This must be possible because this app is doing it, tested it. It is getting the text of each and every app.

    Added Incentive: If you know the answer or things to try, there is another question which looks similar question here.

  • user1406716
    user1406716 about 9 years
    When the 2nd notification (let's say from A in your example) comes in 'sbn.getNotification().extras.getCharSequence(Notification.E‌​XTRA_TEXT).toString(‌​);' is NULL, there is nothing there. Are you not seeing it NULL too? I am trying in Kitkat, API=19
  • user1406716
    user1406716 about 9 years
    I'm not sure if this is true, could you elaborate on how to get the previous Notifications also?: "This way when new sender message comes, previoud notifications also comes and we get callback in NotificationListener"
  • Kushal
    Kushal about 9 years
    For KitKat, it should work fine with AccessibilityService.. Try using this service callback.. In my case, AccessibilityService was working fine till (<= KitKat) so pls check using it
  • Kushal
    Kushal about 9 years
    For getting previous notifications, you can maintain data structure, save incoming notifications to it, parse and display selective notification to your application from it
  • user1406716
    user1406716 about 9 years
    So you tried with AccessibilityService only and not NotificationListenerService??? When using NotificationListenerService, only the TEXT of the first Notification is read, after that all TEXT fields are NULL, so can't save them in my own data structure either. Will AccessibilityService method not working in Lollipop??
  • Kushal
    Kushal about 9 years
    No, accessibility is not working with WhatsApp package.. it is giving null there..
  • user1406716
    user1406716 about 9 years
    So am I understanding you correct that it is not possible to get TEXT of every single notification from Whatsapp then?
  • Kushal
    Kushal about 9 years
    Use Accessibility Service for <= KitKat and use NotificationListener service for > Kitkat.. Then you will able to get text