4

In a Service extending the new (SDK18, JB-4.3) NotificationListenerService, I'd like to get the Notification's status bar icon.

mStatusBarNotification.getNotification().icon returns the resource id of the status bar drawable, but that resource id is naturally not within my app's scope/resources. There's also mStatusBarNotification.getNotification().largeIcon (returning a Bitmap), but that's not set for all notifications and returns the "wrong" icon (the image in the expanded notification drawer).

Nick
  • 3,504
  • 2
  • 39
  • 78

2 Answers2

6

Use getPackageName() on StatusBarNotification to find out the app that posted the Notification. You can then use createPackageContext() to get a Context for that package, then use that Context to retrieve the image (e.g., via getResources()).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • How does that work? setSmallIcon still requires a int resource ID, and using createPackageContext and getResources just gives me access to the Drawable itself. – Kyle Jahnke Sep 07 '14 at 05:31
  • @KyleJahnke: I do not know of a `setSmallIcon()` method that has anything to do with this question. Perhaps you are looking to create a `Notification`; this question is not about creating a `Notification`, but rather examining an existing one. – CommonsWare Sep 07 '14 at 10:46
  • 4
    He's asking how to find the icon that the other notification had entered *into* `setSmallIcon()`; `notification.extras.getInt(Notification.EXTRA_SMALL_ICON)` – Dave Lugg Dec 03 '14 at 03:37
  • @CommonsWare this didn't provide a proper answer. – Nouman Ch Feb 26 '19 at 09:57
  • @NoumanCh: You used the technique that I provided (`createPackageContext()`) in your answer. – CommonsWare Feb 26 '19 at 11:58
  • @CommonsWare Do you know perhaps how to get the image of the notification (like the person image in case it's from a chatting app) ? – android developer Jan 02 '21 at 12:30
  • @androiddeveloper: I have never used `StatusNotification`. I see where you can get the wrapped `Notification` from it, and presumably this data is inside of there. Whether that is available from public fields or is baked into a `RemoteViews` probably depends on OS version, for backwards compatibility. – CommonsWare Jan 02 '21 at 12:36
  • @CommonsWare I think I got it: `notification.getLargeIcon()?.loadDrawable(this)?.toBitmap()` or (I think it does the exact same thing as this ) `notification.extras.getParcelable("android.largeIcon")?.loadDrawable(this)?.toBitmap()` . Not sure though when it's null, when it's some placeholder, when it's the image from the address book, and when it's from the app itself. I guess it depends on the app itself. – android developer Jan 02 '21 at 13:21
1

This is the alternative workaround.

We can get drawable from sbn.getNotification().extras.getInt("android.icon") and then use customview to show this drawable in the notification.

This is how to get Drawable using android.icon value:

            RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_push_notification);
            contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher);
            contentView.setTextViewText(R.id.title, notificationModel.getTitle());
            contentView.setTextViewText(R.id.text, notificationModel.getBody());
           

            try {
                   //now get the context of other app and then get drawable from resoures
                    Drawable drawable1 = context.createPackageContext(notificationModel.getPackageName(), CONTEXT_IGNORE_SECURITY).getDrawable(notificationModel.getIcon());
                    Bitmap bitmap = drawableToBitmap(drawable1);
                    contentView.setImageViewBitmap(R.id.image, bitmap);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }

My notificationModel is:

notificationModel.setKey(sbn.getId());
Prateek Chaubey
  • 166
  • 1
  • 4
  • 16
Nouman Ch
  • 4,023
  • 4
  • 29
  • 42