Я проверял с Build.VERSION, которая больше, чем версия Android Lolipop, с помощью оператора if, чтобы убедиться, что значок может появиться в уведомлении справа.
Я использовал
setLargeIcon ()
, но в уведомлении не отображается значок или изображение.Почему?
Ниже приведен мой код:
String senderImage = "https://cdn.iconscout.com/icon/premium/png-256-thumb/tom-757419.png";
Bitmap bitmap = getBitmapFromURL(senderImage);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, id);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setAutoCancel(true) //Automatically delete the notification
.setLargeIcon(bitmap)
.setChannelId(id)
.setSmallIcon(R.mipmap.ic_app)
.setContentIntent(pendingIntent)
.setContentTitle(notificationTitle)
.setContentText(notificationBody)
.setSound(defaultSoundUri);
} else {
notificationBuilder.setAutoCancel(true) //Automatically delete the notification
.setChannelId(id)
.setSmallIcon(R.mipmap.ic_app)
.setContentIntent(pendingIntent)
.setContentTitle(notificationTitle)
.setContentText(notificationBody)
.setSound(defaultSoundUri);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.notify(1, notificationBuilder.build());
}
Функция преобразования URL в растровое изображение какниже:
public Bitmap getBitmapFromURL(String strURL) {
try {
URL url = new URL(strURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}