Android 如何识别WhatsApp通知是来自安卓棉花糖中的团体还是个人?
我正在为WhatsApp构建聊天机器人,我正在使用notificationListenerService获取通知。 到目前为止,Android 9中的一切都运行良好,我能够发送通知,并且能够识别通知是否来自群组对话。 但当我尝试使用Android 6时,我无法确定通知是否来自群组对话。这对我来说很重要,因为我不想回复小组对话。 这是我正在使用的代码Android 如何识别WhatsApp通知是来自安卓棉花糖中的团体还是个人?,android,service,android-notifications,chatbot,whatsapp,Android,Service,Android Notifications,Chatbot,Whatsapp,我正在为WhatsApp构建聊天机器人,我正在使用notificationListenerService获取通知。 到目前为止,Android 9中的一切都运行良好,我能够发送通知,并且能够识别通知是否来自群组对话。 但当我尝试使用Android 6时,我无法确定通知是否来自群组对话。这对我来说很重要,因为我不想回复小组对话。 这是我正在使用的代码 public void onNotificationPosted(StatusBarNotification sbn) { Log.w(TA
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);
}
}
}
}
}
有一个解决办法。 看到价值了吗
KEY : android.bigText -> name: hi
此处的名称必须是组名。因此,您可以从这里拆分字符串并确定此消息是否来自该组
KEY : android.bigText -> name: hi