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

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.
回答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
回答2:
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();
回答3:
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.
回答4:
Only researched on WhatsApp with Accessibility service permission. all incoming notifications when they get stacked (like in Whatsapp), you can use AccessibilityEvent getRecord() method to extract context in stack. Content contains [sender, to, time, context, index] tested on Android 5.1 on MOTO G
来源:https://stackoverflow.com/questions/29363770/how-to-get-text-of-stacked-notifications-in-android