Glide and NotificationCompat.Builder setLargeIcon ()

How to use Glide in NotificationCompat.Builder setLargeIcon(Bitmap icon) ? I have already studied this tutorial, but I do not want to use it RemoteViews. I also want to use Glide.placeholder(int resource)and Glide.error(int resource)without the use of strategiesGlide.into(new SimpleTarget<Bitmap>(){... });

+6
source share
2 answers

Finally, I did not find a way to this, so I made a strategy Glide.into(new SimpleTarget<Bitmap>(){ ... });that:

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():

Notification notification = notificationBuilder.build();
notificationManager.notify(notificationID, notification);
+1
source

this is how i did it

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 = futureTarget.get()
notificationBuilder.setLargeIcon(bitmap)

Glide.with(this).clear(futureTarget)

notificationManager.notify(0, notificationBuilder.build())

Notification preview

0
source

Source: https://habr.com/ru/post/1683547/


All Articles