【问题标题】:Getting Detail/Expanded Text from an Android Notification?从 Android 通知中获取详细信息/扩展文本?
【发布时间】:2014-03-11 16:31:31
【问题描述】:

我已经实现了一个通知侦听器来查找 Gmail 通知。

我想从通知中收集扩展文本(bigtext),如下图所示:

参见“----Forwarded message---”等,仅在用户展开通知以显示操作按钮时出现。

此字符串值不会出现在通知的“EXTRAS”数据中...

http://developer.android.com/reference/android/app/Notification.html

【问题讨论】:

    标签: android notifications android-notifications


    【解决方案1】:

    查看上述链接后,我进一步研究了捆绑 (EXTRAS) 数据。当你调试它并查看变量时,你会发现所有关于通知的信息都存储在包中,你可以通过

    notifyBundle.extras.getCharSequence("android.textLines") 用于多行通知和

    notifyBundle.extras.getString("android.text") 用于单行通知。

    更多信息,查看eclipse调试模式下的变量

    Image of variables in bundle for single line notification

    Image of variables in bundle for multi line notification

    注意:额外的捆绑包仅在 API19 (Kitkat) 中可用。 API19以下的设备我没试过。

    【讨论】:

      【解决方案2】:

      我在 Android 7 上运行的 Gmail 遇到了类似的问题。

      最终,我意识到(在 another thread 的帮助下)他的解决方案与此处的现有答案不同 - 您正在寻找的内容可以通过不同的方式访问:

      Bundle extras = statusBarNotification.getNotification().extras; extras.get(Notification.EXTRA_BIG_TEXT) == null ? null : extras.get(Notification.EXTRA_BIG_TEXT).toString();

      这样,您将始终获得 String 或 null,即使您要查找的值最初不是字符串。这样做是因为直接调用 getString(Notification.EXTRA_BIG_TEXT) 会返回 null,至少在某些情况下是这样。

      如果还有任何其他值您不确定它们可能存储在哪里,您可以尝试遍历整个附加包,as explained here

      【讨论】:

        【解决方案3】:

        最后,我可以使用这段代码解决这个问题。

        ArrayList<StatusBarNotification> groupedNotifications = new ArrayList<>();
        
        for(StatusBarNotification statusBarNotification : mNotificationManager.getActiveNotifications()) {
            if(statusBarNotification.getNotification().getGroup().equals(NOTIFICATION_GROUP)) {
                  groupedNotifications.add(statusBarNotification);
            }
        }
        
        CharSequence stackNotificationMultiLineText[] = groupedNotifications.get(ZEROTH_INDEX).getNotification().extras.getCharSequenceArray(NotificationCompat.EXTRA_TEXT_LINES);
        

        如果您尝试使用 getCharSequence("android.textLines"),它会返回 null,因为 NotificationCompat.EXTRA_TEXT_LINES 返回的是 CharSequence 数组,而不是单个 CharSequence 对象。

        【讨论】:

        • 你是英雄。
        【解决方案4】:

        我找到了一种访问扩展通知的更短方法。这适用于 API 24。

        如果您在访问getCharSequence("android.textLines") 时收到null,这是因为它实际上返回了一个CharSequence 数组,正如上面Puneet 正确指出的那样。 所以宁愿像这样访问它们:

        if (extras.getCharSequenceArray("android.textLines") != null)
          text = Arrays.toString(extras.getCharSequenceArray("android.textLines"));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-08-19
          • 1970-01-01
          • 2014-03-25
          • 2014-10-15
          • 2021-01-06
          相关资源
          最近更新 更多