Glide: load image to push notifications

Damia Fuentes picture Damia Fuentes · Jan 25, 2017 · Viewed 7.7k times · Source

I am trying to load an image to a push notification using Glide but it says this:

FATAL EXCEPTION: Thread-9730
Process: com.monkingme.monkingmeapp, PID: 24226
java.lang.IllegalArgumentException: You must call this method on the main thread at com.bumptech.glide.util.Util.assertMainThread(Util.java:135)                                                                                

And the code used:

NotificationTarget notificationTarget = new NotificationTarget(
                context,
                rv,
                R.id.remoteview_notification_icon,
                notification,
                NOTIFICATION_ID);

Glide.with(context.getApplicationContext())
     .load(item.getString("cover_img"))
     .asBitmap()
     .placeholder(placeholder)
     .error(placeholder)
     .into(notificationTarget);

I am using MessageHandler from Aerogear --> https://aerogear.org/docs/guides/aerogear-android/push/

The thing is that in a push notification the app is not running, so there isn't a main thread. Any suggestion?

Answer

Divers picture Divers · Jan 25, 2017

Try that way:

    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override 
            public void run() {
                 Glide.with(context.getApplicationContext())
                    .load(item.getString("cover_img"))
                    .asBitmap()
                    .placeholder(placeholder)
                    .error(placeholder)
                    .into(notificationTarget);
        }
    });