How to use Glide into NotificationCompat.Builder setLargeIcon(Bitmap icon)
? I already looked into this tutorial but I don't want to use RemoteViews
. I also want to get use of Glide.placeholder(int resource)
and Glide.error(int resource)
without using the strategy Glide.into(new SimpleTarget<Bitmap>(){ ... });
public class NotificationCompat.Builder. Builder class for NotificationCompat objects. Allows easier control over all the flags, as well as help constructing the typical notification layouts. On platform versions that don't offer expanded notifications, methods that depend on expanded notifications have no effect.
Set the small icon resource, which will be used to represent the notification in the status bar. Set the small icon, which will be used to represent the notification in the status bar and content view (unless overridden there by a large icon ).
To add an image in your notification, pass an instance of NotificationCompat. BigPictureStyle to setStyle() .
You know by now how Glide targets work, thus we won't go over it again. In order to display a large image in the notification, you could use RemoteViews and display a custom notification. The following code creates a custom notification with the layout above for us.
This example demonstrate about How to set Large icon instead of small icon on Android Notification . Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.
This example demonstrates how to integrate Android Notification. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project, and fill all required details to create a new project.
Before Lolipop there was no large icon for notifications. Small icon should be 64x64 and while creating it keep in mind it will be rendered in two colors: white and transparent.
here is how I did this with Glide 4.8.0
val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_message)
.setContentTitle("title")
.setContentText("text")
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val futureTarget = Glide.with(this)
.asBitmap()
.load(photoUrl)
.submit()
val bitmap =
try {
futureTarget.get()
}
catch (e: InterruptedException) {
//set bitmap fallback in case of glide get fail on a 404 response
}
catch (e: ExecutionException) {
//set bitmap fallback in case of glide get fail on a 404 response
}
notificationBuilder.setLargeIcon(bitmap)
Glide.with(this).clear(futureTarget)
notificationManager.notify(0, notificationBuilder.build())
result:
Finally I did not find a way to this so I did the strategy Glide.into(new SimpleTarget<Bitmap>(){ ... });
, which is:
int largeIconSize = Math.round(64 * context.getResources().getDisplayMetrics().density);
GlideApp.with(context)
.asBitmap()
.load(largeIconUrl)
.override(largeIconSize, largeIconSize)
.placeholder(placeHolderResource)
.into(new BaseTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
notificationBuilder.setLargeIcon(resource);
publish();
}
@Override
public void getSize(SizeReadyCallback cb) {
cb.onSizeReady(largeIconSize, largeIconSize);
}
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
super.onLoadFailed(errorDrawable);
notificationBuilder.setLargeIcon(((BitmapDrawable) errorDrawable).getBitmap());
publish();
}
@Override
public void onLoadStarted(@Nullable Drawable placeholder) {
super.onLoadStarted(placeholder);
notificationBuilder.setLargeIcon(((BitmapDrawable) placeholder).getBitmap());
publish();
}
});
and publish()
is:
Notification notification = notificationBuilder.build();
notificationManager.notify(notificationID, notification);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With