Glide:加载图像以推送通知

问题描述 投票:3回答:3

我正在尝试使用Glide将图像加载到推送通知中,但它说:

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)                                                                                

并使用的代码:

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

我正在使用Aerogear的MessageHandler - > https://aerogear.org/docs/guides/aerogear-android/push/

问题是在推送通知中应用程序没有运行,因此没有主线程。有什么建议吗?

android notifications push android-glide remoteview
3个回答
6
投票

试试这个:

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

0
投票

我的解决方案

public class NotificationBuilder {

  public static void build(Context context) {
    ...
    NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(context, channelId)
            .setSmallIcon(R.drawable.app_icon_notification).setContentTitle("Title")
            .setContentText("Description").setAutoCancel(true).setShowWhen(true)
            .setWhen(1574521462).setLights(ledColor, 200, 2000)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setStyle(new NotificationCompat.BigTextStyle().bigText("Description"))
            .setTicker("Description").setSound(defaultSoundUri).setContentIntent(pendingIntent);
    FutureTarget<Bitmap> futureTarget = GlideApp.with(context).asBitmap()
        .load("http://example.com/myImage.jpg")
        .circleCrop().submit();
    LoadImageTask task = new LoadImageTask(icon -> {
          notificationBuilder.setLargeIcon(icon);
          GlideApp.with(context).clear(futureTarget);
          notificationManager.notify(NotificationsCons.SUPPORT_MESSAGES_NOTIFICATION_ID,
              notificationBuilder.build());
        });
    task.execute(futureTarget);
  }
}

private static class LoadImageTask extends AsyncTask<FutureTarget<Bitmap>, Void, Bitmap> {
  private OnSuccess onSuccess;

  interface OnSuccess {
    void onSuccess(Bitmap bitmap);
  }

  LoadImageTask(OnSuccess onSuccess) {
    this.onSuccess = onSuccess;
  }

  @SafeVarargs @Override
  protected final Bitmap doInBackground(FutureTarget<Bitmap>... futureTargets) {
    try {
      return futureTargets[0].get();
    } catch (ExecutionException e) {
      e.printStackTrace();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return null;
  }

  @Override protected void onPostExecute(Bitmap bitmap) {
    super.onPostExecute(bitmap);
    if (bitmap != null)
      onSuccess.onSuccess(bitmap);
  }
}

它工作正常。


0
投票

对于那些可能与最新版本的Glide和Kotlin混淆的人:

val target = NotificationTarget(
            context,
            R.id.iv_image,
            remoteView,
            notification,
            NOTIFICATION_ID)

Glide.with(context.applicationContext)
        .asBitmap()
        .load(url)
        .into(target)

重要的是要注意asBitmap()应该在with(context)之后

© www.soinside.com 2019 - 2024. All rights reserved.