当应用在后台时,Firebase通知不显示图像

时间:2017-08-04 10:01:53

标签: android firebase firebase-notifications

  • 当应用程序位于前台时,我可以成功接收带图像的通知和数据消息。
  • 当app在后台/ kill然后onMessageReceived(消息)没有调用所以我使用getIntent()并且我得到数据但是当app在后台然后android os自动显示使用系统托盘的通知但是图像无法显示。 / LI>
  • 所以我的问题是用图片显示通知文字?

我使用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);
    }
}

1 个答案:

答案 0 :(得分:0)

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

    }
}