如何获得堆叠通知在Android的文字如何获得、文字、通知、Android

2023-09-06 17:53:39 作者:绾起梨花月

现在的问题是如何获得的时候,他们得到堆积(如在WhatsApp的)所有传入的通知文本(而不是所有权)领域。

The question is how to get the TEXT (not title) field of all incoming notifications when they get stacked (like in 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);
}

} 当一个WhatsApp的通知,从单个用户到达此行的第一时间(字符串文本= extras.getCharSequence(android.text)的toString(); )是能够成功读正文,但在那之后,当更多的消息进来,通知得到堆积(如上面所示的画面),变量的文本始终为NULL。

} The first time when a Whatsapp notification arrives from a single user this line (String text = extras.getCharSequence("android.text").toString();) is successfully able to read text, but after that when more messages come in and notifications get stacked (like in the picture shown above), the variable text is always NULL.

这必须是可能的,因为这个程序是做什么的,对其进行了测试。它越来越每一个应用程序的文本。

This must be possible because this app is doing it, tested it. It is getting the text of each and every app.

额外的激励的:如果你知道答案,或可以尝试,另外还有一个问题,看起来类似的问题here.

Added Incentive: If you know the answer or things to try, there is another question which looks similar question here.

推荐答案

WhatsApp的应用程序的结构:

WhatsApp application has structure for sending notification like this :

        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封邮件

Last notification comes with Title as Package Name : WhatsApp and Text as : X messages from Y Conversation

要获得文字:

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

要获得标题:

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

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

To work with this sturcture of stacking, we need to parse this notification stack and display only selective information in our application

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

I hope my answer will help and solve your query