chS
chS

Reputation: 80

Message notifications and pull content

I know that this may possible be a duplicate question, but the other questions don't answer mine.

I am working on an app that pulls notifications (specifically notifications for new WhatsApp messages) from the status bar and reads their content. I have managed to pull the notification title and the message content.

The problem is, when more than one unread message is received, the notification switches from using EXTRA_TEXT to EXTRA_SUMMARY_TEXT (then returning e.g. "2 new messages" instead.

It must possible to separate the messages somehow, seeing as certain existing apps do it (e.g. Snowball combines all messages and displays them in one place, showing message content even when multiple messages have been received and are still unread).

I know that users can send messages via Intents. However, I cannot seem to access incoming intents and have, therefore, assumed that WhatsApp uses explicit intents to send messages.

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

My question:

How can I display all received messages without getting "2 new messages" as content OR is there a better way of doing this?

I need to access message content, the sender's number and the time that the message was received, so that I can save it to a database.

Any help would be appreciated.

Upvotes: 0

Views: 1784

Answers (1)

Kushal
Kushal

Reputation: 8498

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 ----

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

To get Text :

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

To get Title :

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

This answer is given at : enter link description here

Upvotes: 2

Related Questions