Glide:加载图像以推送通知

Dam*_*tes 3 notifications android push remoteview android-glide

我正在尝试使用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)                                                                                
Run Code Online (Sandbox Code Playgroud)

并使用的代码:

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);
Run Code Online (Sandbox Code Playgroud)

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

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

Div*_*ers 7

试试这个:

    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);
        }
    });
Run Code Online (Sandbox Code Playgroud)


May*_*ara 6

这是预期的。由于图像是从 Internet 加载的,因此它应该始终处于 异步调用或后台线程中。您可以使用异步任务或图像加载库,如 Glide。

要从 url 加载图像通知,您可以使用样式“NotificationCompat.BigPictureStyle()”。这需要一个位图(必须从图像 url 中提取)

Glide 的大部分 API 和方法现在都已弃用。下面是使用 Glide 4.9 和高达 Android 10。

 // Load bitmap from image url on background thread and display image notification
        private void getBitmapAsyncAndDoWork(String imageUrl) {

            final Bitmap[] bitmap = {null};

            Glide.with(getApplicationContext())
                    .asBitmap()
                    .load(imageUrl)
                    .into(new CustomTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {

                            bitmap[0] = resource;
                            // TODO Do some work: pass this bitmap
                            displayImageNotification(bitmap[0]);
                        }

                        @Override
                        public void onLoadCleared(@Nullable Drawable placeholder) {
                        }
                    });
        }
Run Code Online (Sandbox Code Playgroud)

显示一次图像通知,位图就准备好了。

private void displayImageNotification(Bitmap bitmap) {

      NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), getChannelId());
            builder
                    .setContentTitle(title)
                    .setContentText(subtext)
                    .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
                    .setSmallIcon(SMALL_ICON)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .setColor(getApplicationContext().getColor(color))
                    .setAutoCancel(true)
                    .setOngoing(false)
                    .setOnlyAlertOnce(true)
                    .setContentIntent(pendingIntent)
                     .setStyle(
                     new NotificationCompat.BigPictureStyle().bigPicture(bitmap))
                    .setPriority(Notification.PRIORITY_HIGH);

        getManager().notify(tag, id, builder.build());
}
Run Code Online (Sandbox Code Playgroud)