2015-03-31 62 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