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


当前回答

如果你正在使用GoogleFireBaseMessaging,你可以在“通知”有效载荷中设置“图标id”(它帮助我解决了白色栏图标的问题):

{
    "to":"<fb_id>",
    "priority" : "high",
    "notification" : 
    {
        "title" : "title",
        "body" : "body" ,
        "sound" : "default",
        "icon" : "ic_notification"
    }
}

将ic_notification设置为R.drawable中自己的id。

其他回答

根据Android设计指南,你必须为builder.setSmallIcon(R.drawable.some_notification_icon);但如果你仍然想显示一个彩色的图标作为通知图标这里是窍门棒棒糖和以上使用下面的代码。largeIcon将作为一个主要的通知图标,您还需要为smallIcon提供一个剪影,因为它将显示在largeIcon的右下角。

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
     builder.setColor(context.getResources().getColor(R.color.red));
     builder.setSmallIcon(R.drawable.some_notification_icon);
     builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));
}

在你的构建器中只使用. setsmallicon (r. mimmap .ic_launcher)。

完全同意用户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

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

另一种选择是利用特定于版本的可绘制(mipmap)目录为Lollipop和以上版本提供不同的图形。

在我的应用程序中,“v21”目录包含带有透明文本的图标,而其他目录包含非透明版本(适用于比棒棒糖更老的Android版本)。

它应该是这样的:

这样,你就不需要检查代码中的版本号,例如:

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.mipmap.ic_notification)
        .setContentTitle(title)
        .setContentText(message)
        .setAutoCancel(true)
        .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

同样,如果您使用“icon”属性,则可以在GCM有效负载中引用“ic_notification”(或您选择的任何名称)。

https://developers.google.com/cloud-messaging/http-server-ref#notification-payload-support

您需要导入单色透明PNG图像。所以你可以设置小图标的图标颜色。否则,它将显示白色在一些设备,如MOTO

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/

希望这能有所帮助