Notification icon in Android 5.0

5

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

The code that generates the push looks 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);

All working perfectly, icon, message, background color, but only until Android Lollipop (5.0), from then on only a white circle remains without loading the image and without the background color.

After much searching, I found answers like these:

  • link

  • link

  • link

  • 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 does not say anything about sizes, about what I need to do to work above the Lollipop SDK.

    Can someone give me a light?

        
    asked by anonymous 30.08.2016 / 20:36

    1 answer

    5

    From Android Lollipop (5.0), Google has made a "small" change in the style of the icons, so that it remains standard on a single color ( and it was warned that previous applications would undergo changes ). I believe it's just a matter of System elegance, exploring more of Material Design , leaving everything "flat". So if you do not do this treatment and use ic_launcher , the system itself will adjust the colors to 5.0 standards. The ideal is to do this 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;
    }
    

    Being R.drawable.icon_silhouette using a single color with .png extension, which will appear in your notification as shown in this question :

    Idealsizeoficon_silhouetteis24x24dp,being:

    [email protected][email protected][email protected]=48.00px

    Changeiconcolor

    Inthe Android 5.0 Behavior Changes documentation, it says that from Lolipop, using the 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 should assume that these icons will be alpha only. The system   draw notification icons and dark gray action icons
    •   

    All instructions for NotificationIcon are provided in the Google Developer Notification Guidelines . p>

    Details

    30.08.2016 / 21:15