如何在 Android Marshmallow 中识别 WhatsApp 通知是来自群组还是个人?

2024-01-03

我正在为 WhatsApp 构建一个聊天机器人,并使用 notificationListenerService 来获取通知。 到目前为止,Android 9 中一切正常,我能够发送通知,并且还能够识别通知是否来自群组对话。 但是当我尝试使用 Android 6 时,我无法识别通知是否来自群组对话。这对我来说很重要,因为我不想回复小组对话。 这是我正在使用的代码

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

在两个设备上打印密钥后,我得到空值bundle.get(NotificationCompat.EXTRA_IS_GROUP_CONVERSATION)在 android 6 中,但不在 android 9 中。 现在我想知道如何识别通知是否来自群组对话。 这是 Android 6 键盘日志

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

有一个解决方法。 查看 的值

KEY : android.bigText -> name: hi

这里的名称必须是组名称。因此,从这里您可以拆分字符串并确定该消息是否来自该组。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Android Marshmallow 中识别 WhatsApp 通知是来自群组还是个人? 的相关文章

随机推荐