编程检测WhatsApp已删除消息:通知监听器筛选问题求助
Hey there! I’ve tackled this exact problem before, so let me walk you through how to filter out only the deleted message notifications from your Notification Listener service—no more sifting through every WhatsApp alert.
Step 1: Filter WhatsApp Notifications First
First things first, narrow down your listener to only process notifications from WhatsApp. You can do this by checking the package name in your onNotificationPosted method:
@Override public void onNotificationPosted(StatusBarNotification sbn) { super.onNotificationPosted(sbn); // Skip all non-WhatsApp notifications if (!"com.whatsapp".equals(sbn.getPackageName())) { return; } // Rest of your logic goes here... }
Step 2: Identify Deleted Message Notification Patterns
WhatsApp has consistent notification text patterns when a message is deleted. Here’s what to look for:
- Personal Chats: The notification text will follow something like
[Contact Name] deleted this message(English) or[联系人昵称] 删除了一条消息(Chinese). The title is usually the contact’s name. - Group Chats: The title will be the group name, and the text will include the member’s name plus the deleted message phrase (e.g.,
[Group Name]: [Member Name] deleted this messagein English).
Step 3: Implement the Filter Logic
Extract the notification’s title and text content, then check for those key phrases. Here’s a code example that handles both English and Chinese (you can expand this for other languages):
@Override public void onNotificationPosted(StatusBarNotification sbn) { super.onNotificationPosted(sbn); if (!"com.whatsapp".equals(sbn.getPackageName())) { return; } Notification notification = sbn.getNotification(); Bundle extras = notification.extras; // Pull title (contact/group name) and text content String notificationTitle = extras.getString(Notification.EXTRA_TITLE); String notificationText = extras.getString(Notification.EXTRA_TEXT); // Check if this is a deleted message notification boolean isDeletedMessage = false; if (notificationText != null) { // Add more language keywords as needed isDeletedMessage = notificationText.contains("deleted this message") || notificationText.contains("删除了一条消息"); } if (isDeletedMessage) { // Do your thing here—save to DB, trigger an alert, etc. Log.d("DeletedWAAlert", "Deleted message from: " + notificationTitle); } }
Edge Cases to Consider
- Multi-Language Support: If your app targets users in different regions, collect the deleted message phrases for each language (e.g.,
eliminó un mensajefor Spanish,ha eliminado un mensajefor Portuguese) and add them to your check. - Notification Format Changes: WhatsApp occasionally tweaks notification layouts. Test regularly to make sure your keyword matches still work, or consider using regex for more flexible matching (e.g.,
.*deleted.*message.*for English). - Admin Deletions in Groups: These follow the same pattern as regular deletions, so your existing logic should catch them without extra work.
Pro Tip
If you want to avoid false positives, you can cross-check with the notification’s icon or other extras, but keyword matching is the most reliable method since WhatsApp doesn’t expose a dedicated "deleted message" notification category.
内容的提问来源于stack exchange,提问作者Tim

