我正在为WhatsApp构建一个聊天机器人,我正在使用notificationListenerService来获取通知。到目前为止,Android 9中的一切都很好,我可以发送通知,也可以识别通知是否来自群组对话。但当我尝试这是安卓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)
的空值,但在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
有一个解决方法。查看的值
KEY : android.bigText -> name: hi
此处的名称必须是组名称。因此,您可以从这里拆分字符串,并确定此消息是否来自该组。