Rohan
Rohan

Reputation: 535

Getting Detail/Expanded Text from an Android Notification?

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:

Expanded text shown in gmail notification

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

Upvotes: 5

Views: 7132

Answers (4)

UzumakiL
UzumakiL

Reputation: 403

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

Upvotes: 0

Yoav Feuerstein
Yoav Feuerstein

Reputation: 1975

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.

Upvotes: 4

Puneet Agarwal
Puneet Agarwal

Reputation: 43

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.

Upvotes: 1

ChiHang
ChiHang

Reputation: 136

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.

Upvotes: 6

Related Questions