Android为我的推送通知图标使用了两种不同的背景颜色,通知图标是否可以始终使用相同的颜色?

bvjveswy  于 2023-03-21  发布在  Android
关注(0)|答案(1)|浏览(126)

我使用Firebase Cloud Messaging发送推送通知。我发现当我的应用程序处于前台模式时,通知图标的背景颜色看起来与三星平板电脑的上半部分显示的通知不同。当我在后台模式下收到推送通知时,图标的颜色也不同。我比较了平板电脑通知面板中的两个图标。
我发现在前台模式下收到推送通知时,通知图标的背景色取决于应用程序图标的颜色。我尝试更改应用程序图标,然后在前台模式下收到推送通知时,通知图标的背景色会自动更改。
我曾尝试用代码更改背景色,但它不适用于. SetColor。
我试过:

int color = Color.Argb(255, 255, 123, 0);
 .SetColor(color)

以及:

.SetColor(Color.Orange)

但是改变颜色是行不通的。

var pendingIntent = PendingIntent.GetActivity(this, Activity1.NOTIFICATION_ID, intent, PendingIntentFlags.Immutable);

  var notificationBuilder = new NotificationCompat.Builder(this, Activity1.CHANNEL_ID)
                                  .SetSmallIcon(Resource.Mipmap.ic_stat_name)
                                  .SetColor(Color.Orange)
                                  .SetContentTitle(Title)
                                  .SetContentText(messageBody)
                                  .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
                                  .SetVibrate(new long[] { 1000, 1000, 0, 0, 0 })
                                  .SetLights(Android.Graphics.Color.Red, 3000, 3000)
                                  .SetPriority((int)NotificationPriority.High)
                                  .SetAutoCancel(true)
                                  .SetContentIntent(pendingIntent);

  var notificationManager = NotificationManagerCompat.From(this);
  notificationManager.Notify(Activity1.NOTIFICATION_ID, notificationBuilder.Build());

通知图标是否可以始终使用相同的背景色?
我发现这个线程和用户有同样的问题,但没有一个真实的的解决方案的问题:Android -如何设置提示通知图标的背景颜色(使用FCM或默认值)?

nx7onnlm

nx7onnlm1#

你会看到不同的图标颜色,因为当你的应用程序在后台时,你的FirebaseMessagingService不会被通知。相反,Firebase会根据你在有效载荷中提供的键值对为你创建通知。这就是它的工作方式。所以你的NotificationCompat.Builder代码不会被调用,.setColor(Color.Orange)也不会产生任何效果。
如FCM文档的Notification Payload部分所示,有两个独立的可选键:iconcolor。创建新消息时,请确保发送这两个值。在这种情况下,图标在前景和背景中看起来应该相同。

相关问题