我正在制作应用程序,我正在从中获取数据
Parse
并将该数据传输到通知以生成并将其显示给用户。
但出于某种原因,我无法在 Marshmallow 中显示正确的彩色图标
在所有其他 Android 版本中,它的工作完全正常,但在 Marshmallow 中,我选择的令人毛骨悚然的白色图标不是实际的。
这是我的通知代码。
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);
请帮我解决这个问题,或者告诉我如何解决这个问题。
首先: 它不是来自棉花糖,通知图标开始从棒棒糖本身变成白色。
Checkout http://developer.android.com/design/style/iconography.html 你会看到白色样式是通知在 Android Lollipop 中的显示方式。
在 Android Lollipop 中,Google 还建议您使用将在(白色)通知图标后面显示的颜色 - https://developer.android.com/about/versions/lollipop/android-5.0-changes
第二:解决方案是将
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))