Show GIF with glide in android notification

Viewed 2004

I have been trying to show a .gif image inside my custom notification using Glide library. I am only able to show static image on the notification panel.

    RemoteViews collapsedView = new RemoteViews(getPackageName(), R.layout.notification_collapsed);
    RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.notification_expanded);

    collapsedView.setTextViewText(R.id.text_view_collapsed_1,"Hello World!");


    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
            .setCustomContentView(collapsedView)
            .setCustomBigContentView(expandedView);

    final Notification notification = mBuilder.build();


    notificationManager.notify(NOTIFICATION_ID,notification);

    notificationTarget = new NotificationTarget(
            getApplicationContext(),
            R.id.image_view_expanded,
            expandedView,
            notification,
            NOTIFICATION_ID
            );

    Glide.with(MainActivity.this)
            .asBitmap()
            .load(R.drawable.giphy)
            .into(notificationTarget);

The drawable is a .gif file. When the notification pops up it shows a static image as I am using .asBitmap(). I tried using

Glide.with(MainActivity.this)
            .asGif()
            .load(R.drawable.giphy)
            .into(notificationTarget);

but i am getting an error "Cannot resolve method 'into(NotificationTarget)'". I have looked around for solutions but i was unable to find one. So how can I show a GIF file with glide in android's notification panel? Is it possible?

2 Answers

Android doesn't support GIF and Videos in notification because notification is built using RemoteViews and RemoteViews doesn't support GIF or Videos.

Also, Glide maintainer answered a similar question here. I am quoting the answer.

It doesn't look like RemoteViews supports setting a Drawable? https://developer.android.com/reference/android/widget/RemoteViews.html.

If there's a way to set an animated drawable in a notification in general, you can probably find a way to do it with a custom Target in Glide.

Indeed. @Somesh Kumar answered your question.

You cannot add GIF's or Videos like he said to your Notifications

In fact, I must ask you: Have you ever seen any notification on your phone that was actively playing a Video or a GIF?

Notifications are not meant to play Videos or GIFs as that would bring some issues as for example higher battery consumption. Notifications should be simple and fast to understand.

Related