Damia Fuentes
Damia Fuentes

Reputation: 5503

Glide and NotificationCompat.Builder setLargeIcon()

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

Upvotes: 9

Views: 3348

Answers (2)

Dan Alboteanu
Dan Alboteanu

Reputation: 10252

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

Upvotes: 7

Damia Fuentes
Damia Fuentes

Reputation: 5503

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

Upvotes: 2

Related Questions