当应用程序处于后台时,Firebase 通知不显示图像
Firebase Notification doesnt show image when app is in background
- 当应用程序在前台时,我可以成功接收带有图像的通知和数据消息。
- 当应用程序在 background/kill 然后 onMessageReceived(message) 没有调用所以我使用 getIntent() 我得到了数据但是当应用程序在后台然后 android os 自动显示使用系统托盘通知但无法显示图像。
- 所以我的问题是显示带图片的通知文本?
我正在使用 django api 发送通知和发送的数据如下
fields = {
'registration_ids': registrationIds,
"notification" : {
"body" : desc,
"title" : name,
"icon" : "https://urbanmatter.com/chicago/wp-content/uploads/2015/04/Girls-Shopping.jpg"
},
"data" : {
"id" : "1",
"body" : desc,
"title" : name,
"image" : "https://urbanmatter.com/chicago/wp-content/uploads/2015/04/Girls-Shopping.jpg"
}
}
下面是我的 onMessageReceived() 方法
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData().get("id"));
id = String.valueOf(remoteMessage.getData().get("id"));
}
if (remoteMessage.getNotification() != null) {
String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
String icon = remoteMessage.getNotification().getIcon();
Intent intent = new Intent(this, AnotherActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("id",id);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText( message);
notificationBuilder.setContentIntent(pendingIntent);
notificationBuilder.setSound(defaultSoundUri);
notificationBuilder.setSmallIcon(R.drawable.jv);
notificationBuilder.setAutoCancel(true);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
ImageRequest imageRequest = new ImageRequest(icon, new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(response));
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}, 0, 0, null, Bitmap.Config.RGB_565,new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
}
});
MySingleton.getmInstance(this).addToRequestQue(imageRequest);
}
}
在您的 FCM 消息中添加这些行
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_face)
.setContentTitle("title")
.setContentText("message").setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap))
.setLargeIcon( getBitmapfromUrl(messageBody.getData().get("**YOUR_IMAGE_URL**")));
public Bitmap getBitmapfromUrl(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
return bitmap;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
- 当应用程序在前台时,我可以成功接收带有图像的通知和数据消息。
- 当应用程序在 background/kill 然后 onMessageReceived(message) 没有调用所以我使用 getIntent() 我得到了数据但是当应用程序在后台然后 android os 自动显示使用系统托盘通知但无法显示图像。
- 所以我的问题是显示带图片的通知文本?
我正在使用 django api 发送通知和发送的数据如下
fields = {
'registration_ids': registrationIds,
"notification" : {
"body" : desc,
"title" : name,
"icon" : "https://urbanmatter.com/chicago/wp-content/uploads/2015/04/Girls-Shopping.jpg"
},
"data" : {
"id" : "1",
"body" : desc,
"title" : name,
"image" : "https://urbanmatter.com/chicago/wp-content/uploads/2015/04/Girls-Shopping.jpg"
}
}
下面是我的 onMessageReceived() 方法
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData().get("id"));
id = String.valueOf(remoteMessage.getData().get("id"));
}
if (remoteMessage.getNotification() != null) {
String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
String icon = remoteMessage.getNotification().getIcon();
Intent intent = new Intent(this, AnotherActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("id",id);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText( message);
notificationBuilder.setContentIntent(pendingIntent);
notificationBuilder.setSound(defaultSoundUri);
notificationBuilder.setSmallIcon(R.drawable.jv);
notificationBuilder.setAutoCancel(true);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
ImageRequest imageRequest = new ImageRequest(icon, new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(response));
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}, 0, 0, null, Bitmap.Config.RGB_565,new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
}
});
MySingleton.getmInstance(this).addToRequestQue(imageRequest);
}
}
在您的 FCM 消息中添加这些行
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_face)
.setContentTitle("title")
.setContentText("message").setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap))
.setLargeIcon( getBitmapfromUrl(messageBody.getData().get("**YOUR_IMAGE_URL**")));
public Bitmap getBitmapfromUrl(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
return bitmap;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}