Problem with notification icon

Asked

Viewed 385 times

3

I know there are several questions related to this. However, I have found nothing with my specific case. I will explain...

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

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 presents error:

inserir a descrição da imagem aqui

I don’t believe the mistake is in the picture, but I could be wrong. I researched the standard android for versions prior to Lollipop and I believe I am acting in the correct way... Mas... I may be wrong again. What would be strange, because android would present the image one way and another not?

Does anyone suggest anything? If you need more information I am available.

Remarks

1 - Even if I create 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.

  • 1

    It may be that it is not going through this function when in the background and using the firebase standard, do the following test replace the .setContentText(message) for .setContentText("Passou por minha função"), push with a different message and see what happens

  • @Icaromartins will check. 1 minute.

  • 1

    @Icaromartins the problem is exactly this.

  • 1

    @Icaromartins I would never imagine... XD .. The message was always displayed!

  • 1

    @Icaromartins thanks man! I wanted to make you win these two little... =)

  • 1

    Now you have two options: 1) you can send the icon by push setting as shown in the answer below. the problem is that it will not arrive in its function. 2) Push in such a way that firebase always calls a function of yours, unfortunately I never did this but maybe this link will help you https://stackoverflow.com/a/37845174/2456894

Show 1 more comment

2 answers

3


After taking the test by changing the

.setContentText(message)

for

.setContentText("Passed by my function")

the function that should be called

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

Knowing that we have two options:

  • You can send the icon by setting the push. The problem here is that if it is essential that the push go through the function, this solution will not solve.

  • Do the push so that the firebase always go through the function. Unfortunately I’ve never done this but maybe this link will help you https://stackoverflow.com/a/37845174/2456894

  • 1

    Again, thanks for the help... I couldn’t take it anymore and couldn’t find the mistake! Hug!

  • 1

    From here 21h I will transfer the 200 to your account ok? kkk It is blocked! Hug!

  • 1

    Quiet man, unfortunately this other way of push I never tried it so I can’t help anymore. Hug!

2

Perform the following Test:

{
  "to": "MYDEVICETOKEN",
  "notification": {
    "body": "Test from postman",
    "icon": "ic_notification"
  }
}
  • How so? Put the icon in the json? I didn’t understand friend... I could explain better?

  • It is an output, you can try using it as well : https://firebase.google.com/docs/cloud-messaging/android/receive

  • And how would I use this json icon in the notification? .setSmallIcon(icon) how?

  • I’ve looked at the documentation several times... But I still don’t understand.

  • Probably when the app is in the background the way to display this icon is different. But I don’t know how.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.