eran tsalach
eran tsalach

Reputation: 84

Android studio - Reading whatsapp notifications problem

I'm developing an android app and part of it needs to read WhatsApp messages by the notifications and analyze them. I'm using a NotificationListenerService and it is working, however, I'm having a problem that notifications are being "analyzed" twice when I get the notification and then get a Message from other chat.

What I want is to analyze each message (Notification) once.

I already tried to save the notification sortKey or the StatusBarNotification key in a HashSet and then check, if it already contains the key every time, however that does not work.

Here is the code that in onNotificationPosted function -

String pack = sbn.getPackageName();
       if (pack.equals("com.whatsapp")) {
            Bundle extras = sbn.getNotification().extras;

       if (extras.getCharSequence("android.text") != null && extras.getString("android.title") != null) {

       if (sbn.getNotification().getSortKey() != null) {
              String title = extras.getString("android.title");
              String text = extras.getCharSequence("android.text").toString();

            //Checking if it's from specic group and analyzing the message and 
            //doing what needs to be done, not related to the problem.                                      

            }
        }
    }

The result I want is that every notification will be analyzed once and not get posted again if some other messages arrive in other chats

Upvotes: 2

Views: 1281

Answers (1)

Martin Marconcini
Martin Marconcini

Reputation: 27236

Without knowing the inner workings of WhatsApp, it's hard to tell what you should exactly do, because it appears they reuse Actions/Extras for various things (which makes sense anyway).

In this case though, you could keep a list/map of the ones you've already "analyzed" and if you get it again, discard it.

Upvotes: 1

Related Questions