Getting Detail/Expanded Text from an Android Notification?

可紊 提交于 2019-11-30 18:23:26

问题


I've implemented a notification listener to look out for a Gmail notification.

I want to collect the expanded text (bigtext) from the notification as shown in the notification below:

See "----Forwarded message---", etc. which only appears when the user expands the notification to show the action buttons.

This string value does not appear in the notification's "EXTRAS" data...

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


回答1:


After viewing the above link, I further investigate the bundle (EXTRAS) data. When you debug it and look at the variable, you can find that all information regards the notification are stored in the bundle and you can get the detail by

notifyBundle.extras.getCharSequence("android.textLines") for multi line notification and

notifyBundle.extras.getString("android.text") for single line notification.

For more information, look at the variable in eclipse debugging mode

Image of variables in bundle for single line notification

Image of variables in bundle for multi line notification

Note: The extra bundle only available in API19 (Kitkat). I've never tried in device which lower than API19.




回答2:


I ran into a similar problem with Gmail running on Android 7.

Eventually, I've realized (with some help from another thread) that he solution was different from the existing answers here - what you're looking for can be accessed in a different way:

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

This way you will always get either a String or null, even if the value you're looking for isn't originally a string. This is done because calling getString(Notification.EXTRA_BIG_TEXT) directly would return null instead, at least in some cases.

If there are any other values you're not sure where they might be stored, you can try iterating through the entire extras bundle, as explained here.




回答3:


Finally, I am able to solve this issue by using this code.

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

If you try to use getCharSequence("android.textLines"), it returns you null because NotificationCompat.EXTRA_TEXT_LINES returns you the array of CharSequence and not a single CharSequence object.




回答4:


I found a shorter way of accessing the expanded notification. This works in API 24.

If you are getting null while accessing getCharSequence("android.textLines"), this is because it actually returns an array of CharSequence as rightly pointed out by Puneet above. So rather access them like this:

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


来源:https://stackoverflow.com/questions/22330809/getting-detail-expanded-text-from-an-android-notification

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