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

橙三吉。 提交于 2019-12-18 10:28:07

问题


I have this code:

Notification notif;

// Build notification
Notification.Builder notifBuilder = new Notification.Builder(context);
notifBuilder.setContentIntent(pendingIntent);
notifBuilder.setContentTitle(title);
notifBuilder.setSmallIcon(icon_resId);
notifBuilder.setContentText(ne.getCaption());
notifBuilder.setDefaults(Notification.DEFAULT_ALL);
notifBuilder.setAutoCancel(autocancel);
notifBuilder.setWhen(System.currentTimeMillis());
notif = notifBuilder.build();

and works fine in Android 4.4.

However, in Android 5.0 the icon showed in status bar is a white square. The icon showed in the new "notification body", that appears when device is locked, is correct.

In http://developer.android.com/reference/android/app/Notification.Builder.html, I don't see anything new about notification icons in API Level 21


回答1:


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."




回答2:


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




回答3:


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.




回答4:


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)



回答5:


Add this in your manifest -

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



回答6:


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




回答7:


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);



回答8:


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



来源:https://stackoverflow.com/questions/27188689/why-do-icons-set-with-notification-builder-setsmallicon-in-android-lollipop-show

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