Problem with notification icon

I know there are several questions related to this. However, I did not find anything with my specific case. I'll explain...

I use the same code to display a notification for both notifications when the app is in the foreground and background. See:

final int icon = R.drawable.ic_stat_name; // aqui está a imagem

        PendingIntent resultPendingIntent;

        resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        resultPendingIntent =
                PendingIntent.getActivity(
                        mContext,
                        0,
                        resultIntent,
                        PendingIntent.FLAG_CANCEL_CURRENT
                );

        final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                mContext, CHANNEL_ID);

        Notification notification;

        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

        inboxStyle.addLine(message);
        notification = mBuilder
                .setSmallIcon(icon)
                .setAutoCancel(true)
                .setContentTitle(title)
                .setContentIntent(resultPendingIntent)
                .setStyle(inboxStyle)
                .setContentText(message)
                .setColor(ContextCompat.getColor(mContext, R.color.colorPrimary))
                .build();
        Log.i("Notificação -> ", "Foi gerada");
        NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID, notification);

And in both cases they show the notification successfully, but only when the application is in the background the image shows error:

insert the description of the image here

I don't believe the error is in the image, but I may be mistaken. I have researched the Android standard for versions prior to lollipop and believe that I am acting in the right way... But... I may be wrong again. What would be strange, because because android would present the image in one way and in another not?

Does anyone suggest anything? If you need more information I will be provision.

Comments

1 - Even I creating an asset vector in xml with android studio the result is exactly the same.

2 - I did tests on several different emulators and the result is the same.

Author: Andrei Coelho, 2018-11-27

2 answers

After taking the test by swapping the

.setContentText (message)

By

.setContentText ("passed through my function")

Of the function that should be called

It was found that firebase was treating push when the application was in background and so the image was not appearing.

Knowing this we have two options:

  • You can send the icon by setting push. Problem in this case, if it is essential that push pass through the function, this solution will not solve.

  • Make push so that firebase always passes through the function. Unfortunately I never did it but maybe this link will help you https://stackoverflow.com/a/37845174/2456894

 3
Author: Icaro Martins, 2018-11-29 21:10:31

Perform the following Test:

{
  "to": "MYDEVICETOKEN",
  "notification": {
    "body": "Test from postman",
    "icon": "ic_notification"
  }
}
 2
Author: Jose Ribamar, 2018-11-29 20:13:31