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


当前回答

另一种选择是利用特定于版本的可绘制(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:targetSdkVersion="20",它应该< 21。

为了避免通知图标变成白色,使用“剪影”图标。透明背景图片。 你可以使用Irfanview来构建它们:

choose a picture, open in IrfanView, press F12 for the painting tools, clean picture if necessary (remove unwanted parts, smooth and polish) Image / Decrease Color Depth to 2 (for a black & white picture) Image / Negative (for a white on black picture) Image / Resize/Resample to 144 x 144 (use Size Method "Resize" not "Resample", otherwise picture is increased to 24 color bits per pixel (24 BPP) again File / Save as PNG, check Show option dialog, check Save Transparent Color, click Save, then click on the black color in the image to set the transparent color

Android似乎只使用drawablexxhdpi图片分辨率(144 x 144),所以复制你的结果ic_notification.png文件到\AndroidStudio\Projects\ app\src\main\res\ drawablexxhdpi。在代码中使用. setsmallicon (R.drawable.ic_notification),或者像Daniel Saidi上面建议的那样使用getNotificationIcon()。

你也可以使用Roman Nurik的Android Asset Studio。

在app gradle中混合这两个东西

 defaultConfig {
    applicationId "com.example.abdulwahidvi.notificationproblem"
    minSdkVersion 16
    //This is the version that was added by project by default
    targetSdkVersion 26 <------ default
    // Changed it to version 20
    targetSdkVersion 20 <------ mine

    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

现在android studio提供了一个插件图像资产,它将在所有需要的drawbale文件夹中生成图标

Image Asset Studio可以帮助您在不同密度下创建各种类型的图标,并向您显示它们将被放置在项目中的确切位置。它包括调整图标和添加背景的工具,同时在预览窗格中显示结果,因此它们完全按照您的预期显示。这些工具可以极大地简化图标设计和导入过程。

你可以通过点击新建>来访问图像资产,点击图像资产选项,它将显示如下窗口

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/

希望这能有所帮助