【发布时间】:2017-04-08 14:58:08
【问题描述】:
我正在使用 fcm 接收数据,我想创建一个包含图像的通知。当我得到这个数据时,我得到了我尝试使用 glide 加载的图像的 url,但 glide 拒绝从服务中触发 [FirebaseMessagingService ]
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private final String TAG = getClass().getName();
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
try {
if (remoteMessage.getData().containsKey("notification")) {
Gson gson = new Gson();
Notif notif = gson.fromJson(remoteMessage.getData().get("notification"), Notif.class);
politicoDataRepository.getPolitico(notif.getPolitico_id())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(politico -> {
createNotifiation(notif, getApplicationContext());
}, throwable -> Log.e("Error", throwable.getMessage() + " " + TAG));
} else {
Log.i("datFirebase", remoteMessage.getFrom() + " " + remoteMessage.getData().get("id"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
realm.close();
}
}
private void createNotifiation(Notif notif, Context context) {
final RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_item);
remoteViews.setImageViewResource(R.id.notif_image, R.mipmap.ic_launcher);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(notif.getTitle())
.setContentText(notif.getMessage())
.setContent(remoteViews)
.setPriority(NotificationCompat.PRIORITY_MIN);
final Notification notification = mBuilder.build();
if (android.os.Build.VERSION.SDK_INT >= 16) {
mBuilder.setCustomBigContentView(remoteViews);
}
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(141, notification);
NotificationTarget notificationTarget = new NotificationTarget(this, remoteViews, R.id.notif_image, notification, 141);
Glide.with(this)
.load(notif.getImage())
.asBitmap()
.into(notificationTarget);
}
}
【问题讨论】:
-
请添加日志,代码写对了。
-
我得到的唯一输出是notif.getId()和notif.getimage(),我没有从滑翔部分得到输出。
-
请在您调用 createNotifiation 方法的地方添加代码。
-
@KeyurThumar 添加了更多代码
-
请检查remoteMessage是否获取图片url
标签: android firebase firebase-cloud-messaging android-glide firebase-notifications