NotificationCompat.setLargeIcon(Bitmap)未显示

时间:2018-03-13 10:02:06

标签: android android-notifications

我正在使用通知。但方法setLargeIcon(Bitmap)不起作用。这是我的代码段:

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());
}

无论我在 setSmallIcon 之前放置 setLargeIcon ,它仍然无效。它只显示 small和largeIcon smallIcon 。我的大图标是矢量绘图

2 个答案:

答案 0 :(得分:1)

你使用矢量drawable只显示小图标,如果大图标然后设置png或任何显示的大图像。但矢量drawable图标只显示小图标。

  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());
            }
        }

答案 1 :(得分:0)

如果使用矢量图像资源,则必须获得如下的位图:

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;
}

然后在您的构建器中:

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