2015-03-31 60 views
7

问题是如何在堆叠时(如在Whatsapp中)获取所有传入通知的TEXT(不是标题)字段。如何在Android中获取堆叠通知的文本

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通知从单个用户到达此行中的第一次(字符串文本= extras.getCharSequence( “android.text”)的toString();)已成功读取文本,但在此之后,当有更多消息进入并且通知堆积(如上图所示)时,变量文本始终为NULL。

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

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

回答

14

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的和文本为:Y的会话X的消息

要获取文字:

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

要获得标题:

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

要使用此内在张力结构堆叠的工作,我们需要分析该通知栈和我们的应用程序只显示选择性信息

我希望我的回答可以帮助解决您的查询

+0

当第二个通知(比如来自A的例子)进入'sbn.getNotification()。extras。getCharSequence(Notification.EXTRA_TEXT)的ToString();”是NULL,那里什么也没有。你是不是也看到它了?我正在尝试Kitkat,API = 19 – user1406716 2015-03-31 09:00:54

+0

我不确定这是否属实,您是否可以详细说明如何获得以前的通知?:“*这种方式当新的发送者消息发出时,previoud通知也会出现,并且我们会得到回调在NotificationListener *“ – user1406716 2015-03-31 09:01:34

+0

对于KitKat,它应该与AccessibilityService正常工作..尝试使用此服务回调..在我的情况下,AccessibilityService正常工作,直到(<= KitKat),所以请检查使用它 – Kushal 2015-03-31 09:07:46

0

只研究了WhatsApp的与辅助功能服务权限。 所有传入的通知在堆叠时(如Whatsapp),您可以使用AccessibilityEvent getRecord()方法来提取堆栈中的上下文。 内容包含[发送到,时间,情境,指数]在Android 5.1测试Moto G机种

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

如果你与Android 7.0+,WhatsApp的工作使用MessageStyle扩展通知。这里 - 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