Why do icons set with Notification.Builder.setSmallIcon in Android Lollipop show as a white square?

独自空忆成欢 提交于 2019-11-29 22:01:17

Look at the documentation: http://developer.android.com/design/style/iconography.html

there are words: "Notification icons must be entirely white. Also, the system may scale down and/or darken the icons."

I have resolved changing the icon size to 16x16 px and using only white color

CoDeSigns

As noted in Android 5.0 Behavior Changes of the Android Developers site under Notifications:

Notifications are drawn with dark text atop white (or very light) backgrounds to match the new material design widgets. Make sure that all your notifications look right with the new color scheme. If your notifications look wrong, fix them:

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

http://developer.android.com/about/versions/android-5.0-changes.html.

Tobliug

Duplicate : Notification bar icon turns white in Android 5 Lollipop

In a Brief :

Android 5 update : https://developer.android.com/about/versions/android-5.0-changes.html Notifications -> Material design style

Update or remove assets that involve color. The system ignores all non-alpha channels in action icons and in the main notification icon. You should assume that these icons will be alpha-only. The system draws notification icons in white and action icons in dark gray.

It's possible to set the small icon background color using (default is gray) :

Notification.Builder#setColor(int)

Add this in your manifest -

 <meta-data android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/ic_notification" />

In Android 5.0 the icon showed in the status bar is a white square because of 5.0 Lollipop "Notification icons must be entirely white".

You can easily find this types of icons on the Material icon. Visit: https://material.io/icons/

Google also suggests that we use a custom color that will be displayed behind the white notification icon using setColor() method.

For more information visit: https://developer.android.com/about/versions/android-5.0-changes.html

Anyone still looking at this, the simplest way of getting your icon to display correctly is first rendering it with the Android Icon Studio here:

https://romannurik.github.io/AndroidAssetStudio/icons-notification.html

Unzip the files from the downloaded zip into your project /main folder, so they slot into the relevant drawable-xxxx folders.

Then, to change colour in the notification use something like this:

NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
    .setSmallIcon(R.drawable.ic_notification_appicon) // <-- Icon from Android Icon Studio 
    .setColor(context.getColor(R.color.holo_blue))    // <-- Set your preferred icon colour to appear in the notification dropdown list
    .setContentTitle("Title")
    .setContentText("Content")
    .setAutoCancel(true)
    .setCategory(NotificationCompat.CATEGORY_EVENT)
    .setDefaults(Notification.DEFAULT_ALL)
    .setPriority(NotificationCompat.PRIORITY_DEFAULT);
范文锋

Remove the android:targetSdkVersion="21" from manifest.xml. it will work!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!