NotificationListenerService get Notification Icon?

家住魔仙堡 提交于 2019-11-30 10:27:53

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()).

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.getPackageNmae(), 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.setPackageNmae(sbn.getPackageName());
        notificationModel.setTitle(sbn.getNotification().extras.getString("android.title"));
        notificationModel.setBody(sbn.getNotification().extras.getString("android.text"));
        notificationModel.setIcon(sbn.getNotification().extras.getInt("android.icon"));
        notificationModel.setKey(sbn.getId());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!