Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 611
Damia Fuentes Avatar asked Aug 11 '17 17:08

Damia Fuentes


People also ask

What is NotificationCompat builder in Android?

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.

How do I make my notification icons smaller?

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

How do you set a notification picture on android?

To add an image in your notification, pass an instance of NotificationCompat. BigPictureStyle to setStyle() .

How to display a large image in a glide notification?

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.

How to set large icon instead of small icon on Android notification?

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.

How to integrate notification in Android Studio?

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.

What is the size of notification icon in Lolipop?

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.


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

like image 145
Dan Alboteanu Avatar answered Nov 09 '22 18:11

Dan Alboteanu


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);
like image 39
Damia Fuentes Avatar answered Nov 09 '22 19:11

Damia Fuentes