Android Marshmallow 中的通知未显示颜色图标

Android Notification is not showing Colour Icon in Marshmallow

我正在制作应用程序,我正在从中获取数据Parse并将该数据传输到 Notification 以生成并显示给用户。

但由于某些原因,我无法在 Marshmallow 中显示正确的彩色图标

在所有其他 Android 版本中,它的工作完全正常,但在 Marshmallow 中,它令人毛骨悚然的白色图标不是实际的,我 select。

这是我的通知代码。

 Intent cIntent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                cIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentText(data)
                .setContentTitle("Notification from Parse")
                .setContentIntent(pendingIntent);

        Notification notification = builder.build();
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(100, notification);

请帮我解决这个问题,或者告诉我如何解决这个问题。

首先:它不是来自 Marshmallow,通知图标开始从 Lollipop 本身变成白色。

结帐 http://developer.android.com/design/style/iconography.html 您会看到白色样式是通知在 Android Lollipop 中的显示方式。

在 Android Lollipop 中,Google 还建议您使用将显示在(白色)通知图标后面的颜色 - https://developer.android.com/about/versions/android-5.0-changes.html

第二个:解决方案是将 LargeIcon 设置为 Notification Builder

Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setLargeIcon(largeIcon)
                .setContentText(data)
                .setContentTitle("Notification from Parse")
                .setContentIntent(pendingIntent);

然后,您的通知将如下所示:

您还可以使用.setColor()设置通知图标的背景颜色。

在某些型号(如 Pixel 等)上,您只能添加一种颜色,只需添加:

.setColor(ContextCompat.getColor(context, R.color.your_color))