bumptech / bumptech/glide

NotificationTarget failed in BroadcastReceiver(BroadcastReceiver中NotificationTarget 无法加载图片)

Open
#4,436 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
35k
Forks
6.2k
Avg merge
1d 11h
Merged PRs (30d)
8

Description

i want to create a notification with remoteviews in a BroadcastReceiver,some code like this:
===========translation in chinese==start===========
我想要在BroadcastReceiver的onReceive方法中显示一个自定义布局的通知,代码如下:
===========translation in chinese===end==========
`
final RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.remoteview_notification);

rv.setImageViewResource(R.id.remoteview_notification_icon, R.mipmap.future_studio_launcher);

rv.setTextViewText(R.id.remoteview_notification_headline, "Headline");
rv.setTextViewText(R.id.remoteview_notification_short_message, "Short Message");

// build notification
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.future_studio_launcher)
.setContentTitle("Content Title")
.setContentText("Content Text")
.setContent(rv)
.setPriority( NotificationCompat.PRIORITY_MIN);

final Notification notification = mBuilder.build();

// set big content view for newer androids
if (android.os.Build.VERSION.SDK_INT >= 16) {
notification.bigContentView = rv;
}

NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, notification);
private NotificationTarget notificationTarget = new NotificationTarget(
context,
rv,
R.id.remoteview_notification_icon,
notification,
NOTIFICATION_ID);
String portrait="http://xxxxxxxxxxxxx";
Glide
.with( context.getApplicationContext() ) // safer!
.load( portrait )
.asBitmap()
.into( notificationTarget );`

BUT !!!!!
this code will work at any Activity or Fragement Onclike Event,but failed in a method "onReceive(Context context, Intent intent) " of BroadcastReceiver ,the image won't display but just a blank,it seems like some glide options such as .error() .placeholder() does not work either
i also have tried somecode like this:
===========translation in chinese==start===========
但是,在BroadcastReceiver的onReceive中,这段代码中的图片并不能加载,总是一片空白,而且貌似有些配置比如 .error() 、.placeholder()等都失效,然而在Activity or Fragement中随便一个点击事件中就是正常的。。。
同时我还尝试了如下代码:
===========translation in chinese===end==========
`Glide.with(getContext()).asBitmap().load(portrait).into(new CustomTarget() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) {
LogUtils.e("onResourceReady");
}

@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
LogUtils.e("onLoadCleared");
}

@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
LogUtils.e("onLoadFailed");
}
});`

in a BroadcastReceiver ,glide will always callback onLoadCleared,does not call onResourceReady or onLoadFailed
but onResourceReady will callback in Activity or Fragement
i also have tried somecode like this:
===========translation in chinese==start===========
但是,在BroadcastReceiver的onReceive中,总是回调onLoadCleared,onResourceReady和onLoadFailed都不会回调,
然而在Activity 或 Fragement中,可以正常回调onResourceReady
同时我还尝试了如下代码:
===========translation in chinese===end==========
`
new Thread(()->{
try {
String portrait="http://xxxxxxxxxxxxx";
LogUtils.e("start get");
Bitmap bitmap = Glide.with(getContext()).asBitmap().load(portrait).submit().get();
LogUtils.e("end get"+bitmap.getWidth());
} catch (Exception e) {
e.printStackTrace();
}
}).start();`

these code will block on get() method and "end get" will not print forever,it happened both in BroadcastReceiver and Activity or Fragement
===========translation in chinese==start===========
这段代码会永远卡在Bitmap bitmap = Glide.with(getContext()).asBitmap().load(portrait).submit().get();这一行,"end get"永远也不会输出,在BroadcastReceiver 和 Activity or Fragement中都一样。
===========translation in chinese===end==========

PS: i have tried on glide version 4.9.0 and 4.11.0
i am an chinese user phone type HuaWei nova 4e,EMUI version:10.0.0 , android version: 10
===========translation in chinese==start===========
我使用了glide的2个版本尝试都失败:4.9.0 and 4.11.0
我的测试机型: HuaWei nova 4e,EMUI version:10.0.0 , android version: 10
===========translation in chinese===end==========

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.