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


当前回答

我也遇到了太多的问题,但在互联网上搜索后,我找到了这个问题的不同解决方案。让我总结一下所有的解决方案并解释一下:

注意:此解决方案适用于Phonegap cordova用户

例子

<预览

您需要将android-targetSdkVersion值设置为小于21。 所以在设置这个值后,通知图标图像将出现到Android 6(棉花糖),它不会在Android 7(牛轧糖)工作。 这个解决方案对我很有效。

更改配置文件中的statusbarStyle。 例子

<预览

但这个解决方案只有在应用程序打开时才有效。 所以,我想这个解决方案不是最好的解决方案,但它适用于许多用户。

让你的图标透明。 这个方法对很多人都有效。 实际上,在本地应用程序的开发中,我们需要为他们提供三个图像: (一)应用程序图标 (b)通知图标 (c)状态栏图标图像,但在混合移动应用程序开发的情况下,没有这样做的选项。 所以让你的图标透明,这个解决方案将解决你的问题。

我相信上述解决方案之一将为您的问题工作。

其他回答

从manifest.xml中删除android:targetSdkVersion="21"它会起作用的! 从这一点上看,在你的apk中没有任何问题,这只是一个技巧,我应用这个,我在通知中发现了彩色图标

alpha-channel是Android用于通知图标的图像的唯一数据:

Alpha == 1:像素显示为白色 alpha == 0:像素显示为您选择的Notification.Builder#setColor(int)

https://developer.android.com/about/versions/android-5.0-changes.html上提到了这一点:

系统会忽略操作图标和主通知图标中的所有非alpha通道。您应该假设这些图标都是纯alpha的。

几乎所有的内置绘图似乎都适合alpha图像,所以你可能会使用类似的东西:

Notification.Builder.setColor(Color.RED)
                    .setSmallIcon(android.R.drawable.star_on)

但我仍在寻找官方证实这一点的API文档。

在Android 22上测试。

另一种选择是利用特定于版本的可绘制(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

我也遇到过同样的问题,那是因为我的应用通知图标不平坦。对于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());
}

错误的图标

正确的图标

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

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

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