我有一个应用程序显示自定义通知。问题是在Android 5中运行时,通知栏中的小图标显示为白色。我该如何解决这个问题?


当前回答

Post android Lollipop release android has changed the guidelines for displaying notification icons in the Notification bar. The official documentation says "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.” Now what that means in lay man terms is "Convert all parts of the image that you don’t want to show to transparent pixels. All colors and non transparent pixels are displayed in white"

你可以通过这里的截图看到如何做到这一点的细节 https://blog.clevertap.com/fixing-notification-icon-for-android-lollipop-and-above/

希望这能有所帮助

其他回答

完全同意用户Daniel Saidi的观点。为了有颜色的NotificationIcon,我写这个答案。

为此,你必须创建像剪影这样的图标,并使某些部分透明,无论你想添加颜色的地方。也就是说,

你可以使用

.setColor (your_color_resource_here)

注意:setColor只在Lollipop中可用,所以,你必须检查OSVersion

if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    Notification notification = new Notification.Builder(context)
    ...
} else {
    // Lollipop specific setColor method goes here.
    Notification notification = new Notification.Builder(context)
    ...
    notification.setColor(your_color)
    ...            
}

您也可以使用Lollipop作为目标SDK来实现这一点。

关于NotificationIcon的所有说明均在谷歌开发人员控制台通知指南行中给出。

首选通知图标大小24x24dp

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

更多信息请参考通知图标大小的链接。

供您参考:如果图标没有出现,请确保您的本地或远程通知配置包含正确的图标名称

'largeIcon' => 'ic_launcher',
'smallIcon' => 'ic_launcher' // defaults to ic_launcher, 

根据文档,从Android 3.0 (API Level 11)开始,通知图标必须是白色的:

https://developer.android.com/guide/practices/ui_guidelines/icon_design_status_bar

“状态栏图标是由透明屏幕上的白色像素组成的 背景,使用alpha混合用于光滑的边缘和内部 适当的纹理。”

我也遇到过同样的问题,那是因为我的应用通知图标不平坦。对于android版本,你的应用通知图标应该是扁平的,不要使用带有阴影的图标。

下面的代码在所有android版本上都运行得很好。

private void sendNotification(String msg) {

    NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent = new Intent(this, CheckOutActivity.class);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(getString(R.string.app_name))
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg).setLights(Color.GREEN, 300, 300)
            .setVibrate(new long[] { 100, 250 })
            .setDefaults(Notification.DEFAULT_SOUND).setAutoCancel(true);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(new Random().nextInt(), mBuilder.build());
}

错误的图标

正确的图标

通知是灰色的,如下所述。不管别人写了什么,它们都不是黑白分明的。你可能见过带有多种色调的图标,比如网络强度条。

在API 21 (Lollipop 5.0)之前,彩色图标可以工作。您可以强制应用程序以API 20为目标,但这会限制应用程序可用的特性,因此不建议这样做。您可以测试正在运行的API级别,并适当地设置颜色图标或灰度图标,但这可能不值得。在大多数情况下,最好使用灰色图标。

图像有四个通道,RGBA(红/绿/蓝/ alpha)。对于通知图标,Android忽略R、G和B通道。唯一重要的通道是Alpha,也称为不透明度。用编辑器设计图标,让您可以控制绘图颜色的Alpha值。

Alpha值如何生成灰度图像:

Alpha = 0(透明)-这些像素是透明的,显示背景颜色。 Alpha = 255(不透明)-这些像素是白色的。 Alpha = 1…254 -这些像素正是你所期望的,提供透明和白色之间的阴影。

使用setColor更改:

Call NotificationCompat.Builder.setColor(int argb). From the documentation for Notification.color: Accent color (an ARGB integer like the constants in Color) to be applied by the standard Style templates when presenting this notification. The current template design constructs a colorful header image by overlaying the icon image (stenciled in white) atop a field of this color. Alpha components are ignored. My testing with setColor shows that Alpha components are not ignored; instead, they still provide greyscale. Higher Alpha values turn a pixel white. Lower Alpha values turn a pixel to the background colour (black on my device) in the notification area, or to the specified colour in the pull-down notification. (It seems others have reported slightly different behavior, so be aware!)