如何在Android中获取堆叠通知的文本

如何在Android中获取堆叠通知的文本,android,android-notifications,Android,Android Notifications,问题是如何在所有传入通知堆叠时获取它们的文本(而不是标题)字段(如Whatsapp) } 当单个用户第一次收到Whatsapp通知时,这一行(String text=extras.getCharSequence(“android.text”).toString();)能够成功地读取文本,但在这之后,当更多的消息进入并堆积通知时(如上图所示),变量text始终为空 这必须是可能的,因为我们正在进行测试。它获取每个应用程序的文本 额外的激励:如果您知道答案或需要尝试的东西,那么还有一个问题与此类似

问题是如何在所有传入通知堆叠时获取它们的文本(而不是标题)字段(如Whatsapp)

} 当单个用户第一次收到Whatsapp通知时,这一行(String text=extras.getCharSequence(“android.text”).toString();)能够成功地读取文本,但在这之后,当更多的消息进入并堆积通知时(如上图所示),变量text始终为空

这必须是可能的,因为我们正在进行测试。它获取每个应用程序的文本


额外的激励:如果您知道答案或需要尝试的东西,那么还有一个问题与此类似。

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();
要使用这种堆栈结构,我们需要解析此通知堆栈,并在应用程序中仅显示选择性信息


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

仅在具有无障碍服务许可的WhatsApp上进行研究。 所有传入通知在堆叠时(如在Whatsapp中),您可以使用AccessibilityEvent getRecord()方法提取堆栈中的上下文。
内容包含在MOTO G上的Android 5.1上测试的[sender、to、time、context、index],我认为这可以帮助您:

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

如果您使用的是Android 7.0+,WhatsApp将使用MessageStyle扩展通知。这里-

从通知中检索所有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*/

            }
        }
    }
Bundle extras=mysbn.getNotification().extras;
if((Build.VERSION.SDK\u INT>=Build.VERSION\u code.N)){
包裹b[]=(包裹[])额外获取(通知.额外消息);
如果(b!=null){
content=“”;
用于(可包裹tmp:b){
Bundle msgBundle=(Bundle)tmp;
content=content+msgBundle.getString(“文本”)+“\n”;
/*设置io=msgBundle.keySet();//以获取此捆绑包可用的密钥*/
}
}
}

与我的答案相同。

当第二个通知(假设在您的示例中来自A)出现在'sbn.getNotification().extras.getCharSequence(notification.EXTRA_TEXT.toString();'是空的,那里什么都没有。你不是也看到了吗?我正在Kitkat中尝试,API=19我不确定这是不是真的,您能详细说明如何获得以前的通知吗?:“这样,当新发件人消息出现时,以前的通知也会出现,并且我们在NotificationListener中得到回调”对于Kitkat,它应该可以与AccessibilityService配合使用。。尝试使用此服务回调。。在我的例子中,AccessibilityService一直运行良好,直到(为了获取以前的通知,您可以维护数据结构,将传入的通知保存到它,解析并显示iTunes Accessibility Service For Kitkat为您的应用程序提供的选择性通知。)然后您就可以获取文本
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*/

            }
        }
    }