消息通知和提取内容

时间:2015-04-17 13:47:37

标签: android android-intent android-notifications whatsapp

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

我正在开发一个应用程序,用于从状态栏中提取通知(特别是针对新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"作为内容的情况下显示所有收到的消息,或者有更好的方法吗?

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

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:2)

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和Text as:来自Y Conversation的X消息

获取文字:

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

获得标题:

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

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

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

答案见:enter link description here