从通知 parcelable 对象中读取内容以进行后续通知

Read content from notification parcelable objects for consequent notification

我正在尝试构建 Whatsapp 通知过滤应用程序,我在其中监控来自 Whatsapp 的所有通知并根据过滤策略删除消息。

我可以使用下面的 link 代码获取消息内容 Extract notification text from parcelable, contentView or contentIntent 仅针对第一条消息

但问题是我只能获取第一条消息,如果用户没有阅读第一条消息,那么第二条消息之后我只会收到“来自发件人的 2 条消息”,而不是实际消息。

注意:我得到

android.text = 第一条消息的实际消息,但从第二条 message/notification 开始为空 android.title = 发件人 android.summaryText = "n new messages"

如有任何帮助,我们将不胜感激。

是的,经过几个小时的谷歌搜索,我终于设计了一个适合我的代码。

Bundle extras = sbn.getNotification().extras;
    CharSequence[] lines = extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES);
    JSONArray s = new JSONArray();
    for (CharSequence msg : lines) {
                    msg = removeSpaces(msg);
                    if (!TextUtils.isEmpty(msg)) {
                        s.put(msg.toString());
                    }
                }
    private static String removeSpaces(@Nullable CharSequence cs) {
            if (cs == null)
                return null;
            String string = cs instanceof String ? (String) cs : cs.toString();
            return string.replaceAll("(\s+$|^\s+)", "").replaceAll("\n+", "\n");
        }

此处 JSONArray 包含我想要的所有消息