如何在Android中获取Stacked Notifications的文本

时间:2015-03-31 08:14:41

标签: android android-notifications

问题是如何在堆叠时获取所有传入通知的TEXT(非标题)字段(如在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);
}

} Whatsapp通知第一次从单个用户到达此行( String text = extras.getCharSequence(" android.text")。toString(); )成功能够阅读文字,但在此之后,当有更多邮件进入并且通知被堆叠时(如上图所示),变量 text 始终为NULL。

这一定是可能的,因为this app正在进行测试。它正在获取每个应用程序的文本。

添加奖励:如果你知道答案或要尝试的事情,还有另一个问题看起来类似问题here

3 个答案:

答案 0 :(得分:17)

WhatsApp应用程序具有发送通知的结构:

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

最后一次通知标题为包名:WhatsApp和Text as:来自Y Conversation的X消息

获取文字:

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

获得标题:

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

要使用这种堆叠结构,我们需要解析此通知堆栈并在我们的应用程序中仅显示选择性信息

我希望我的回答有助于解决您的问题

答案 1 :(得分:1)

我认为这可以帮到你:

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();

答案 2 :(得分:0)

如果您使用的是Android 7.0+,WhatsApp会使用MessageStyle Expanded Notifications。在这里 - https://developer.android.com/training/notify-user/expanded.html#message-style

等通知中检索所有5条消息
MyFriend (5 messages)
testt

这样做:

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

            }
        }
    }

与我的回答here相同。