Java Android颜色通知图标
我正在开发一个为用户创建通知的应用程序。我希望图标在状态栏中显示为白色,但在下拉通知菜单中显示为蓝色。下面是一个谷歌商店应用程序做同样事情的例子 状态栏中的白色通知: 下拉菜单中的彩色通知: 我如何复制这个?我必须设置哪些属性 编辑: 这是我当前的代码-我将图像设置为全白色,背景透明,因此在状态栏中看起来很好,但在通知下拉列表中,图像仍然是相同的白色:Java Android颜色通知图标,java,android,colors,push-notification,android-notifications,Java,Android,Colors,Push Notification,Android Notifications,我正在开发一个为用户创建通知的应用程序。我希望图标在状态栏中显示为白色,但在下拉通知菜单中显示为蓝色。下面是一个谷歌商店应用程序做同样事情的例子 状态栏中的白色通知: 下拉菜单中的彩色通知: 我如何复制这个?我必须设置哪些属性 编辑: 这是我当前的代码-我将图像设置为全白色,背景透明,因此在状态栏中看起来很好,但在通知下拉列表中,图像仍然是相同的白色: private NotificationCompat.Builder getNotificationBuilder() {
private NotificationCompat.Builder getNotificationBuilder() {
return new NotificationCompat.Builder(mainActivity)
.setDeleteIntent(deletedPendingIntent)
.setContentIntent(startChatPendingIntent)
.setAutoCancel(true)
.setSmallIcon(R.drawable.skylight_notification)
.setColor(ContextCompat.getColor(mainActivity, R.color.colorPrimary))
.setContentTitle(mainActivity.getString(R.string.notification_title))
.setContentText(mainActivity.getString(R.string.notification_prompt));
}
构建通知时,可以设置颜色和图标如果您的图标是纯白色图像,它将在正确的位置为您应用颜色
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notificationId = 10 // Some unique id.
// Creating a channel - required for O's notifications.
val channel = NotificationChannel("my_channel_01",
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT)
manager.createNotificationChannel(channel)
// Building the notification.
val builder = Notification.Builder(context, channel.id)
builder.setContentTitle("Warning!")
builder.setContentText("This is a bad notification!")
builder.setSmallIcon(R.drawable.skull)
builder.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
builder.setChannelId(channel.id)
// Posting the notification.
manager.notify(notificationId, builder.build())
}
您可以使用
DrawableCompat.setTint(int-drawable)在设置可绘制文件之前,可绘制文件的代码>。
然后执行mutate()
可绘制,否则颜色将应用于该可绘制的每个实例以下是我为我的应用程序所做的
private void showNotification(Context context) {
Log.d(MainActivity.APP_TAG, "Displaying Notification");
Intent activityIntent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(R.drawable.ic_notification);
mBuilder.setColor(Color.GREEN);
mBuilder.setContentIntent(pendingIntent);
mBuilder.setContentTitle("EarthQuakeAlert");
mBuilder.setContentText("It's been a while you have checked out earthquake data!");
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
带颜色的样品:
不带颜色的样品:
我在这里找到了问题的答案:
我不完全知道问题出在哪里,但是通过把我用来制作图标的巨大png放到这个工具中
通过将生成的图标放入我的mipmap文件夹中,我可以使setColor(…)
属性正常工作。对于从控制台发送的firebase Nofications,您只需将其添加到清单中:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/white_logo" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/custom_color" />
其中,白色标志是您的应用程序的白色标志,自定义标志颜色是您希望为图标和文本着色的颜色
这里有更多详细信息:如果您想在推送通知或内置通知中根据gmail和twitter更改颜色和标题名称,则需要在通知中添加这些行
builder.setSmallIcon(R.drawable.skull)
builder.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
第一行用于图标,第二行您需要定义颜色使用Android Studio本身提供的“Asset Studio”创建通知图标(右键单击res文件夹和New>Image Asset)
然后设置通知的颜色
int color = Color.argb(255, 228, 14, 18);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_notification)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setColor(color)
.setPriority(NotificationCompat.PRIORITY_HIGH);
我参加聚会可能会迟到,但以上所有答案都不相关或不推荐
使用NotificationCompat.Builder
例如:
val builder = NotificationCompat.Builder(this, "whatever_channel_id")
.setSmallIcon(R.drawable.ic_notification) //set icon for notification
.setColor(ContextCompat.getColor(this, R.color.pink))
.setContentTitle("Notification Title")
.setContentText("Notification Message!")
现在它将以粉红色显示通知
注意:
如果您使用的是firebase,则不会直接看到颜色。您必须将其添加到清单文件中
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_notification" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/pink" />
不幸的是,更改颜色对我所做的只是更改通知下拉列表中图像旁边文本的颜色,而不是更改图像的颜色。您是否使用纯白色图标作为小图标?它应该在需要的时候涂上颜色。它是纯白色的,背景是透明的,保存为pngI编辑了我的文章,并在O上测试了一些代码,上面的代码可以使我的图标和文本在下拉时都是我的color\u primary
,在不下拉时都是纯白色。希望这会有更多帮助。上帝保佑你的狗。这是我今晚需要的答案。我建议不要这样做,否则状态栏中会有一个彩色图标。我能够解决此问题-请查看下面的答案。谢谢!我花了很长时间试图弄清楚为什么我的通知图标在通知横幅中仍然显示为白色,最后,通过该工具,我的通知图标正确地使用了横幅中的颜色(同时在状态栏中仍然为白色),并将此颜色用于到这里并使用photoshop生成图标的所有人(或任何其他平面设计工具)。不要使用CMYK模式!在创建/导出图标时使用RGB-这在我的案例中很有帮助。我使用了此工具,我的图像是纯白色透明PNG,我确保它位于RGB颜色空间中,但是SetColor
仍然不起作用。我缺少什么?对于仍然卡住的任何人,请尝试缩小图像的大小。72x72图标对我不起作用,但48x48图标对我起作用。按照“Oblivionkey3”的建议添加了图标,但在添加后将图标颜色设置为自定义。在此之前,Andriod Oreo通知上的图标颜色为灰色(状态栏中的图标显示为白色)。什么是custom_color
?如何设置?另请参阅:@mukesh.kumar,custom_color是您想要使用的任何颜色。您只需在颜色文件中声明即可