Glide and NotificationCompat.Builder setLargeIcon()

Viewed 3035

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

2 Answers

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:

enter image description here

Related