消息通知和拉取内容

Message notifications and pull content

我知道这可能是一个重复的问题,但其他问题没有回答我的问题。

我正在开发一个从状态栏中提取通知(特别是新 WhatsApp 消息的通知)并读取其内容的应用程序。我成功拉取了通知标题和消息内容。

问题是,当收到多个未读消息时,通知从使用 EXTRA_TEXT 切换到 EXTRA_SUMMARY_TEXT(然后返回例如 "2 new messages"

必须有可能以某种方式分隔消息,就像某些现有的应用程序那样(例如,Snowball 将所有消息组合在一起并将它们显示在一个地方,即使收到多条消息但仍未读时也会显示消息内容)。

我知道用户可以通过 Intents 发送消息。但是,我似乎无法访问传入的意图,因此假设 WhatsApp 使用显式意图发送消息。

Intent i = new  Intent("com.test.testapp.NOTIFICATION_LISTENER");

        Bundle extras = sbn.getNotification().extras;

        if(sbn.getPackageName().contains("com.whatsapp"))
        {
            String title = extras.getString(Notification.EXTRA_TITLE);
            String summary = extras.getString(Notification.EXTRA_SUMMARY_TEXT);
            String msg = extras.getString(Notification.EXTRA_TEXT);

            if(msg != null)
            {
                i.putExtra("notification_event", msg);
            }
            else
            {
                i.putExtra("notification_event", summary);
            }

        }
        else
        {
            i.putExtra("notification_event","...");
        }
        sendBroadcast(i);

我的问题:

如何在不将 "2 new messages" 作为内容的情况下显示所有收到的消息,或者有更好的方法吗?

我需要访问消息内容、发件人号码和收到消息的时间,以便将其保存到数据库中。

如有任何帮助,我们将不胜感激。

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

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

希望我的回答对您有所帮助并解决您的疑问

此答案给出于:enter link description here