Notification icon in Android 5.0

Asked

Viewed 707 times

5

I’m creating a push notification system with firebase in my project.

The code that generates the push is like this:

notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setColor(getResources().getColor(R.color.colorPrimary))
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

Everything works perfectly, icone, message, background color, however, only until Android Lollipop (5.0), from there is only a white circle without loading the image and without the background color.

After a lot of research, I found answers like these:

  1. https://stackoverflow.com/questions/28387602/notification-bar-icon-turns-white-in-android-5-lollipop

  2. https://stackoverflow.com/questions/30795431/icon-not-displaying-in-notification-white-square-shown-instead

  3. https://blog.clevertap.com/fixing-notification-icon-for-android-lollipop-and-above/

But none of them really helped me, one of the answers says to do a validation of the SDK, like this:

private int getNotificationIcon() {
    boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
    return useWhiteIcon ? R.drawable.icon_silhouette : R.drawable.ic_launcher;
}

But it doesn’t say anything about sizes, about what I need to do to make it work above the Lollipop SDK.

Someone can give me a light?

  • Link 3 gives the answer.

  • Strange, from what I understand he says to do the png white color with hollow background only, but I’ve always made all the notification icons like this, and it doesn’t work for anything from 5.0.1 on.

  • I have never tested in 5+, but what I understand is that anything that is not transparent is put in white. Read the note on Androidassetstudio under the word "source"

1 answer

5


Starting with Android Lollipop (5.0), Google made a "small" change in the style of the icons, so that it remains standard in a single color (and was warned that previous applications would change). I believe that precisely for the sake of elegance of the system, exploiting more of the Material Design, leaving everything "stoned". So if you don’t do this treatment and use the ic_launcher, the system itself will adjust the colors according to the standards 5.0. Ideally do so below, as you showed in the question:

private int getNotificationIcon() {
    boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
    return useWhiteIcon ? R.drawable.icon_silhouette : R.drawable.ic_launcher;
}

Then R.drawable.icon_silhouette using a single colour with extension .png, which will appear in your notification as shown in this matter:

inserir a descrição da imagem aqui

Ideal size of icon_silhouette is 24x24dp, being:

mdpi    @ 24.00dp   = 24.00px
hdpi    @ 24.00dp   = 36.00px
xhdpi   @ 24.00dp   = 48.00px

Change color of icon

In the documentation in Android 5.0 behavior changes, says that from Lolipop, with the use of Material Design style, the developer should follow the following recommendations:

  • Use setColor() to set a highlight color in a circle behind of the icon image.
  • Update or remove assets that involve color. The system ignores all non-alpha channels on action icons and the main notification icon. You must assume that these icons will be alpha only. The system draws notification icons and dark gray action icons

All instructions concerning the NotificationIcon are provided in Google Developer Notification Guidance.

Details

  • Thanks Cleidimar, my doubt was in the creation of this icon_silhouette, thank you!

Browser other questions tagged

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