How to get text of Stacked Notifications in Android

后端 未结 4 1358
囚心锁ツ
囚心锁ツ 2020-12-15 23:53

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

4条回答
  •  抹茶落季
    2020-12-16 00:22

    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 io = msgBundle.keySet(); // To get the keys available for this bundle*/
    
                }
            }
        }
    

    Same as my answer here.

提交回复
热议问题