我正在创建一个通知管理应用程序,我想获取其他应用程序显示的通知内容.目前我使用这样的代码:
statusBarNotification.getNotification().extras.getString(Notification.EXTRA_TITLE);
还有这个 :
statusBarNotification.getNotification().extras.getString(Notification.EXTRA_TEXT);
阅读通知的标题和文本.但几个小时后,我找不到一种方法来获取通知文本附带的图像.例如,在Whatsapp的通知中显示的个人资料图片.我知道这不是小图标或大图标,我查了几次.
所以,如果有人能以任何方式提供帮助,我们将不胜感激
解决方法
我假设您使用
NotificationListenerService
来收听其他应用程序的通知.
在NotificationService类中,在额外的Notification.EXTRA_SMALL_ICON中提取图标ressource id并访问其他app包ressources以获取Drawable.
Notification.EXTRA_PICTURE包含通知中发送的大图像:
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 } }