【问题标题】:How to identify if WhatsApp notification is coming from a group or an individual in Android Marshmallow?如何识别 WhatsApp 通知是来自 Android Marshmallow 中的群组还是个人?
【发布时间】:2020-09-16 04:46:33
【问题描述】:

我正在为 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);
                }
            }
        }       
    }
}

在两个设备上打印密钥后,我在 android 6 中得到了 bundle.get(NotificationCompat.EXTRA_IS_GROUP_CONVERSATION) 的 null 值,但在 android 9 中没有。 现在我想知道如何确定通知是否来自群组对话。 这是 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

【问题讨论】:

    标签: android service android-notifications chatbot whatsapp


    【解决方案1】:

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

    KEY : android.bigText -> name: hi
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-01
      • 2016-01-10
      • 1970-01-01
      • 2013-05-24
      相关资源
      最近更新 更多