Search code examples
androidfirebasefirebase-notifications

Firebase Notification doesnt show image when app is in background


  • I can successfully receive notification and data messages with image when the app is in the foreground.
  • when app in background/kill then onMessageReceived(message) doesn't called so am using getIntent() and I got data but when app in background then android os automatically show notification using system tray but image can't show.
  • So my question is to show notification text with image?

I am using django api for sending notification and sent data as below

    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"
        }
    }

Below is my onMessageReceived() method

@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);
    }
}

Solution

  • Add these line in your FCM Messaging

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