Reliable way of retrieving StatusbarNotification details (title, notification text)
Asked Answered
H

2

14

I'd like to get as much information out of a StatusBarNotification-object as possible. Right now, the only "reliable" information that can be accessed is the tickerText-property. I'm using the following code to get the notification's title and text via RemoteViews, but a lot of the time, the title and/or text will simply be null :-(:

    //Get the title and text
    String mTitle = "";
    String mText = "";
    try {
        RemoteViews remoteView = sbn.getNotification().contentView;
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ViewGroup localView = (ViewGroup) inflater.inflate(remoteView.getLayoutId(), null);
        remoteView.reapply(getApplicationContext(), localView);
        TextView tvTitle = (TextView) localView.findViewById(android.R.id.title);
        TextView tvText = (TextView) localView.findViewById(16908358);
        mTitle = (String)tvTitle.getText();
        mText = (String)tvText.getText();
    } catch (Exception e){
        Log.e(TAG, "Error getting notification title/text: " + e);
    }

Is there any alternative (more reliable) way? I could "hand-code" the resource IDs for "popular" notifications like Gmail, SMS, etc., but this may break at any time when those apps are updated. Thanks!

Harlan answered 8/8, 2013 at 9:15 Comment(0)
K
4

With Android 4.4(KitKat), API Level 19 you can use Notification.extras attibute to get Notification title,text,....

http://gmariotti.blogspot.com/2013/11/notificationlistenerservice-and-kitkat.html

Kenyatta answered 3/12, 2013 at 7:16 Comment(0)
Z
3

Checking resource IDs is actually how TalkBack, the Android screen reader, parses notification types. It attempts to load IDs directly from various packages.

Check the source on Google Code for a full example. Here is a snippet:

private static int ICON_GMAIL;

private static boolean sHasLoadedIcons = false;

private static void loadIcons(Context context) {
    ...

    ICON_GMAIL = loadIcon(context, "com.google.android.gm",
        "com.google.android.gm.R$drawable", "stat_notify_email");

    sHasLoadedIcons = true;
}

public static NotificationType getNotificationTypeFromIcon(Context context, int icon) {
    if (!sHasLoadedIcons) {
        loadIcons(context);
    }

    ...

    if (icon == ICON_GMAIL) {
        return NotificationType.EMAIL;
    }
}
Zoochore answered 29/8, 2013 at 5:3 Comment(4)
Thanks, alanv, that's really interesting! I didn't think that this would be an "acceptable" approach, since the names (and int-IDs) of resources in other packages can change all the time. I'll try to manually dig-up the resource IDs of the most popular app's title/summary notification textfields, then.Harlan
@Harlan any ideas on how you would do that? Also, A lot of the times exceptions are thrown when trying to inflate the layout resource id.Severus
@Niek, I'll share my solution on here once it's done. I get the layout-inflation-exceptions, too :-/. I've seen another solution where they iterate over the layout TextView ids (without inflating the actual layout beforehand). I'll see if I can find it again.Harlan
Not yet, sorry, Niek. I haven't gotten round to implementing this yet.Harlan

© 2022 - 2024 — McMap. All rights reserved.