Obl*_*ey3 34 java android colors push-notification android-notifications
我正在开发一个应用程序,我在那里为用户创建通知.我希望图标在状态栏中显示为白色,但在下拉通知菜单中显示时显示为蓝色.以下是Google Store应用程序执行相同操作的示例.
状态栏中的白色通知:
下拉菜单中的彩色通知:
我怎么能复制这个?我需要设置哪些属性?
编辑: 这是我当前的代码 - 我使图像全白,透明背景,所以它在状态栏中看起来很好,但在通知中,图像仍然是相同的白色:
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));
}
Run Code Online (Sandbox Code Playgroud)
Obl*_*ey3 24
我在这里找到了我的问题的答案:https://stackoverflow.com/a/44950197/4394594
我不完全知道问题是什么,但是将我用于图标的巨大png放入此工具中https://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.type= image&source.space.trim = 1&source.space.pad = 0&name = ic_skylight_notification
,通过将生成的图标放入我的mipmap文件夹,我能够使该setColor(...)属性正常工作.
rad*_*aun 17
对于从控制台发送的firebase nofitications,您只需在清单中添加:
<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" />
Run Code Online (Sandbox Code Playgroud)
white_logo是您的应用程序白色徽标,custom_color是您希望图标和文本着色的颜色.
有关详细信息,请访问:https://firebase.google.com/docs/cloud-messaging/android/client
JRG*_*JRG 14
这是我为我的应用做的...
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());
}
Run Code Online (Sandbox Code Playgroud)
带颜色的样品:
构建通知时,可以设置颜色和图标。如果您的图标是纯白色图像,它将在正确的位置为您应用颜色。
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())
}
Run Code Online (Sandbox Code Playgroud)
我可能会迟到,但以上所有答案都不相关或已弃用。
您可以使用setColor以下方法轻松实现这一点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!")
Run Code Online (Sandbox Code Playgroud)
现在它会将通知显示为粉红色
注意: 如果您使用 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" />
Run Code Online (Sandbox Code Playgroud)