Android – how do I get an image from another application’s notification?
I'm creating a notification management application. I want to get the notification content displayed by other applications. At present, I use this Code:
statusBarNotification.getNotification().extras.getString(Notification.EXTRA_TITLE);
And this:
statusBarNotification.getNotification().extras.getString(Notification.EXTRA_TEXT);
Read the title and text of the notice. But after a few hours, I couldn't find a way to get the image attached to the notice text. For example, the profile picture displayed in WhatsApp's notice. I knew it wasn't a small icon or a large icon. I checked it several times
Therefore, if anyone can help in any way, we will be very grateful
resolvent:
I assume you use notificationlistenerservice to listen to notifications from other applications
In the notificationservice class, in the additional notification.extra_ SMALL_ Extract the icon resssource ID from icon and access other app packages to get drawable
Notification.EXTRA_ Picture contains the large image sent in the notification:
public class NotificationService extends NotificationListenerService {
Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
@Override
public void onNotificationPosted(StatusBarNotification statusBarNotification) {
// a notification is posted
String pack = statusBarNotification.getPackageName();
Bundle extras = statusBarNotification.getNotification().extras;
int iconId = extras.getInt(Notification.EXTRA_SMALL_ICON);
try {
PackageManager manager = getPackageManager();
Resources resources = manager.getResourcesForApplication(pack);
Drawable icon = resources.getDrawable(iconId);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (extras.containsKey(Notification.EXTRA_PICTURE)) {
// this bitmap contain the picture attachment
Bitmap bmp = (Bitmap) extras.get(Notification.EXTRA_PICTURE);
}
}
@Override
public void onNotificationRemoved(StatusBarNotification statusBarNotification) {
//call when notification is removed
}
}