Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get text of Stacked Notifications in Android

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.

like image 910
user1406716 Avatar asked Mar 31 '15 08:03

user1406716


People also ask

How do I stack notifications on Android?

To create a stack, call setGroup() for each notification you want in the stack and specify a group key. Then call notify() to send it to the wearable.

How do I show group notifications on Android?

The user can tap the group summary notification to open your app. On Android 7.0 and higher, the system shows your group summary notification as a nested group of notifications, labeled with snippets of text from each grouped notification; it does not display the text you set on the group summary notification.

How do I get text notifications on my Android?

Text Message Notification Settings - Android™Tap 'Settings' or 'Messaging' settings. If applicable, tap 'Notifications' or 'Notification settings'. Configure the following received notification options as preferred: Enabled when a checkmark is present or switch is in the ON position.

How do I read full text notifications on Android?

1 I can just "pull down" the notification (using one finger) or pinch-zoom it (using two fingers) to see the full text: first, pull down the notification bar. then pull down the single notification, where the text is cropped.


2 Answers

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

like image 103
Kushal Avatar answered Sep 20 '22 05:09

Kushal


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.

like image 36
Balamurugan Avatar answered Sep 21 '22 05:09

Balamurugan