我正在通过if语句检查大于android Lolipop版本的Build.VERSION,以确保图标可以出现在通知的右侧。
我用过
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;
}
}