Krot
Krot

Reputation: 197

NotificationCompat.setLargeIcon(Bitmap) is not showing

I am working with Notification. But the method setLargeIcon(Bitmap) did not work. This is my code snippet:

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationCompatBuilder = new NotificationCompat.Builder(this, "chanel1103")
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_music_player))
            .setSmallIcon(android.R.drawable.ic_lock_idle_alarm)
            .setContentTitle(getString(R.string.noti_title))
            .setContentText(getString(R.string.noti_content));
            .setSubText(getString(R.string.noti_sub_text));

if (notificationManager != null) {
      notificationManager.notify(1, notificationCompatBuilder.build());
}

No matter I put setLargeIcon right before setSmallIcon, it still did not work. It only showed the smallIcon for both small and largeIcon. My large icon is a vector drawable

Upvotes: 3

Views: 4770

Answers (2)

Sviatoslav Zaitsev
Sviatoslav Zaitsev

Reputation: 992

If you use vector image resources you have to get bitmap like this:

public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
    Drawable drawable = ContextCompat.getDrawable(context, drawableId);
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
            drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

And then in your builder:

.setLargeIcon(getBitmapFromVectorDrawable(this, R.drawable.R.drawable.ic_music_player))

Upvotes: 5

user4571931
user4571931

Reputation:

you used vector drawable is show only small icon if large icon then set png or any large image that is show. but vector drawable icon show only small icon.

  NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            NotificationCompat.Builder notificationCompatBuilder = new NotificationCompat.Builder(DbInsert.this, "chanel1103")
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.a)) // set a png or jpg images
                    .setSmallIcon(android.R.drawable.ic_lock_idle_alarm) //set vector drawable is work.
                    .setContentTitle("HHHHHHHHHHH")
                    .setContentText("GGGGGGGGGGGGG")
        .setSubText("DFGGG");

            if (notificationManager != null) {
                notificationManager.notify(1, notificationCompatBuilder.build());
            }
        }

Upvotes: 1

Related Questions