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

use*_*716 11 android android-notifications

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

在此输入图像描述

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);
}
Run Code Online (Sandbox Code Playgroud)

第一次Whatsapp通知从单个用户到达时,这一行(String text = extras.getCharSequence("android.text").toString();)成功地能够读取文本,但之后会有更多消息进入和通知堆叠(如上图所示),变量文本始终为NULL.

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

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

Kus*_*hal 18

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 ----
Run Code Online (Sandbox Code Playgroud)

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

获取文字:

sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TEXT).toString();
Run Code Online (Sandbox Code Playgroud)

获得标题:

sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TITLE).toString();
Run Code Online (Sandbox Code Playgroud)

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

我希望我的回答能够帮助解决您的疑问