Read content from notification parcelable objects for consequent notification

為{幸葍}努か 提交于 2019-12-04 14:31:17

问题


I am trying to build Whatsapp Notification filtering app, where I monitor all notification from Whatsapp and remove messages as per filtering policy.

I can fetch message content using below link code Extract notification text from parcelable, contentView or contentIntent for first message only

but the problem is I can fetch only first message, if user does not read first message then second message onwards I get only "2 messages from sender" instead of actual message.

NOTE: I am getting

android.text = actual message for first message but its null from second message/notification onwards android.title = sender android.summaryText = "n new messages"

any help would be appreciated.


回答1:


Yes, finally after few hours of googling I design a code which does work for me.

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");
        }

here JSONArray s contains all messages that I want



来源:https://stackoverflow.com/questions/27745342/read-content-from-notification-parcelable-objects-for-consequent-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!