【问题标题】:Read content from notification parcelable objects for consequent notification从通知可打包对象中读取内容以获取后续通知
【发布时间】:2015-01-02 16:35:55
【问题描述】:

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

我可以使用以下链接代码获取消息内容 Extract notification text from parcelable, contentView or contentIntent 仅用于第一条消息

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

注意:我得到了

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

任何帮助将不胜感激。

【问题讨论】:

  • 我面临同样的问题@ghmulchandani。你有解决办法吗?
  • @SmartphoneDeveloper,它解决了你的问题吗?

标签: android notifications whatsapp


【解决方案1】:

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

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 包含我想要的所有消息

【讨论】:

  • 我会试一试,一定会给我反馈。
  • 你能解释一下你第一行的额外内容吗,你能提供完整的代码吗。我也试过,只能得到第一条消息。
  • @Ravi,我刚刚为你编辑了代码。您需要将此代码放在 NotificationListenerService 的 onNotificationPosted 中
  • 感谢@ghmulchandani 的更新,我会试试的。
  • 最后的字符串包含的数据是 String string = cs instanceof String ? (字符串) cs: cs.toString(); ?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-11
  • 2012-09-13
相关资源
最近更新 更多