How to identify if WhatsApp notification is coming from a group or an individual in Android Marshmallow?

Viewed 586

I'm building a chatbot for WhatsApp and I am using the notificationListenerService to get the notifications. So far everything is working fine in Android 9 and I'm able to send the notification and also able to identify if the notification is from the Group Conversation or not. But when I try this is Android 6 I'm unable to identify if the notification is from a group conversation or not. And this is important for me since I don't want to reply to the Group Conversation. Here is the code I'm using

public void onNotificationPosted(StatusBarNotification sbn) {
    Log.w(TAG, "onNotificationPosted: ");
    if(!sbn.getPackageName().equals(WHATSAPP_PKG) || sbn.isOngoing()) return;

    Notification notification = sbn.getNotification();
    if (notification != null) {
        Bundle bundle = notification.extras;

        Log.d(TAG, "onNotificationPosted: Print Keys");
        for(String k : bundle.keySet()) {
            Log.d(TAG, "KEY : " + k + " -> " + bundle.get(k));
        }
        ArrayList<RemoteInput> remoteInputs = getRemoteInputs(notification);

        Object isGroupConversation = bundle.get(NotificationCompat.EXTRA_IS_GROUP_CONVERSATION);
        Object hiddenConversationTitle = bundle.get(NotificationCompat.EXTRA_HIDDEN_CONVERSATION_TITLE);
        String conversationTitle = bundle.getString("android.conversationTitle");

       
        Log.d(TAG, "onNotificationPosted: isGP " + isGroupConversation);//always null for API 23
        Log.d(TAG, "onNotificationPosted: hideCovTitle " + hiddenConversationTitle);//always null for API 23
        Log.d(TAG, "onNotificationPosted: convoTitle " + conversationTitle); //always null for API 23

        if (isGroupConversation != null) {
            boolean isGroup = (((boolean) isGroupConversation) && (conversationTitle != null));//Group Params
            Log.d(TAG, "isGroupConversation: " + isGroup);//This is working fine in android 9 but not in Android 6
        }else{
            Log.e(TAG, "is GroupConversation : NULL ");
        }

        String title = bundle.getString(NotificationCompat.EXTRA_TITLE);
        Object msz = bundle.get(NotificationCompat.EXTRA_TEXT);
        if(msz != null && title != null){
            if(msz.equals("Hi")) {
                Log.d(TAG, "onNotificationPosted: msz HI");
                if (!remoteInputs.isEmpty()){
                    Log.d(TAG, "onNotificationPosted: remote inputs " + remoteInputs.toString());
                    sendMessage(notification,"Hey there!!", bundle, remoteInputs);
                }
            }
        }       
    }
}

After printing the keys on both the device I am getting null value for bundle.get(NotificationCompat.EXTRA_IS_GROUP_CONVERSATION) in android 6 but not in android 9. Now I wanted to know How can I identify the is the notification is from a group conversation or not. Here is the Android 6 KeyLog

onNotificationPosted: Print Keys
    KEY : android.title -> temp
    KEY : android.subText -> null
    KEY : android.template -> android.app.Notification$BigTextStyle
    KEY : android.showChronometer -> false
    KEY : android.icon -> 2131231578
    KEY : android.text -> vijat: hi
    KEY : android.progress -> 0
    KEY : android.progressMax -> 0
    KEY : android.showWhen -> true
    KEY : android.rebuild.applicationInfo ->ApplicationInfo {1aaef78 com.whatsapp}
    KEY : android.people -> [Ljava.lang.String;@98eb651
    KEY : android.largeIcon -> android.graphics.Bitmap@f67eb6
    KEY : android.bigText -> name: hi
    KEY : android.infoText -> null
    KEY : android.wearable.EXTENSIONS -> Bundle[mParcelledData.dataSize=936]
    KEY : android.originatingUserId -> 0
    KEY : android.progressIndeterminate -> false
    KEY : android.summaryText -> 1 new message
1 Answers

There is a workaround for that. See the value of

KEY : android.bigText -> name: hi

Here the name must be the group name. so from here you can split the string and identify if this message is from the group or not.

Related