skip to Main Content

I use Firebase Cloud Messaging to send push notifications. I found out that the background color of my notification icon looks different when my application is in foreground mode and when the notification is displayed in the upper part of my Samsung tablet. When I receive a push notification in background mode then the color of the icon is different. I compared the two icons in my tablet’s notification panel.

I found out that the background color of the notification icon depends on my application icon’s colors when the push notification is received in foreground mode. I have tried to change my application icon and then automatically the background color of the notification icon changes when the push notification is received in foreground mode.

I have tried to change the background color with code but it is not working with .SetColor.

I have tried it with:

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

And with:

.SetColor(Color.Orange)

But changing the color is not working.

  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());

Is it possible to always use the same background color for the notification icon?

I found this thread and the user has the same issue but there is not a real solution for the issue: Android – How to set heads-up notification icon background color (using FCM or default value)?

2

Answers


  1. Chosen as BEST ANSWER

    When the push notification is received in foreground mode then the notification is displayed in the upper part of my Samsung tablet. The notification icon background color is automatically a color that is used in the app icon. Therefore, I use the following workaround so that the background color of my notification icon(the icon from the notification that was in the upper part of my device) is (almost) the same color as the notification icon that is displayed in my tablet's notification panel.

    -First I made a screenshot on my device when the notification is displayed in foreground mode in the upper part of my Samsung tablet.

    -After that I imported the screenshot in the vector graphics software Inkscape and I used the color picker tool on my icon on the screenshot to get the icon background color.

    -Finally I use this color in AndroidManifest.xml and in .SetColor so that the icon background color is (almost) the same in my tablet's notification panel and when the notification is displayed in foreground mode.

    AndroidManifest.xml:

    <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/custom_color" />
    

    colors.xml:

    <resources>
    <color name="custom_color">#4cb3fc</color>
    </resources>
    

    .SetColor:

    int color = Color.Argb(255, 76, 178, 252);
    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)
                                  .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);
    

  2. You see different icon colors because when you application is in background your FirebaseMessagingService is not being notified. Instead Firebase creates notification on your behalf depending on key-value pairs which you provide in payload. This is just the way it works. So your code with NotificationCompat.Builder won’t be called and .setColor(Color.Orange) doesn’t make any effect.

    As Notification Payload section of FCM documentation indicates, there are two separate optional keys: icon and color. Just make sure that you are sending both values when creating a new message. In this case icons should look identical in both foreground and background.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search