How to get Stacked Notifications text on Android

The question is how to get the TEXT field (not the header) of all incoming notifications when they fit (for example, into 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 a Whatsapp notification comes from one user, this line ( String text = extras.getCharSequence ("android.text"). ToString (); ) can read the text successfully, but after that, when more messages arrive, and notifications become complex (for example, in the figure above), the variable text is always NULL.

This should be possible because this application does this, tested it. It receives the text of each application.

Added incentive: if you know the answer or try something, another question arises, which looks like a similar question here .

+7
android android-notifications
source share
4 answers

WhatsApp has a structure for sending notifications as follows:

  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 ---- 

The last notification comes with a header as the package name: WhatsApp and Text as: X messages from Y Conversation

Get text:

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

Get the name:

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

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

I hope my answer helps and resolves your request.

+14
source share

I think this may 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(); 
+1
source share

Only WhatsApp study with permission to access. all incoming notifications when they are stacked (for example, in Whatsapp), you can use the AccessibilityEvent getRecord () method to retrieve the context on the stack. Content contains [sender, to, time, context, index] tested on Android 5.1 on MOTO G

0
source share

If you are running Android 7.0+, WhatsApp uses advanced MessageStyle notifications. Here - https://developer.android.com/training/notify-user/expanded.html#message-style

To get all 5 messages from a notification, for example

 MyFriend (5 messages) testt 

Do it:

 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 .

0
source share

All Articles