Android Color Notification Icon

72,812

Solution 1

I found the answer to my question here: https://stackoverflow.com/a/44950197/4394594

I don't know entirely what the problem was, but by putting the huge png that I was using for the icon into the this tool https://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.type=image&source.space.trim=1&source.space.pad=0&name=ic_skylight_notification and by placing the generated icons it gave into my mipmap folder, I was able to get the setColor(...) property to work correctly.

Solution 2

For firebase nofitications sent from console you just need to add this in your manifest:

    <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" />

Where white_logo is your app white logo, and custom_color is the color you want to have the icon and text colored.

More details here: https://firebase.google.com/docs/cloud-messaging/android/client

Solution 3

Here is what I did for my app ...

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

Sample With Color:

enter image description here

Sample without Color: enter image description here

Solution 4

When building the notification, you can set the color and the icon. If your icon is a pure white image, it'll apply the color for you in the correct spots.

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

Solution 5

I might be late to the party but all above answers are not relevant or deprecated.

enter image description here

You can achieve this easily by using setColor method of NotificationCompat.Builder

Example:

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!")

Now it will show notification as pink color

Note: If you are using firebase then the color won't be seen directly. You have to add this in manifest file.

<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" />
Share:
72,812
Oblivionkey3
Author by

Oblivionkey3

Updated on July 08, 2022

Comments

  • Oblivionkey3
    Oblivionkey3 almost 2 years

    I'm working on an app where I create a notification for the user. I want the icon to appear as white when it's in the status bar, but colored blue when it's being displayed in the drop down notification menu. Here's an example of the same thing being done by the Google Store app.

    White notification in status bar:

    enter image description here

    Colored notification in drop down menu:

    enter image description here

    How can I replicate this? What properties do I have to set?

    Edit: Here's my current code - I made the image all white with a transparent background, so it looks fine in the status bar, but in the notification drop, the image is still the same white color:

    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));
        }
    
  • Oblivionkey3
    Oblivionkey3 almost 7 years
    Unfortunately all that changing the color did for me was change the color of the text next to the image in the notification drop down, not change the color of the image at all.
  • advice
    advice almost 7 years
    Are you using a pure white icon as your small icon? It should colour that when it needs to.
  • Oblivionkey3
    Oblivionkey3 almost 7 years
    It's pure white with a transparent background, and it's saved as a png
  • advice
    advice almost 7 years
    I edited my post, and tested some code on O, and the code above works for making my icon and text both my color_primary when pulled down, and just pure white when not. Hopefully that'll help more.
  • advice
    advice almost 7 years
    I'd advise against this or else you'll have a coloured icon within the status bar.
  • Oblivionkey3
    Oblivionkey3 almost 7 years
    I appreciate your help, but I managed to find solve this using another method which I just added to this page.
  • advice
    advice almost 7 years
    Cool, glad you could figure it out.
  • Andrew Leader
    Andrew Leader over 6 years
    THANK YOU! I spent way too long trying to figure out why my notification icon was still appearing white in the notification banner, and finally with that tool my notification icon correctly uses the color in the banner (while still being white in the status bar)
  • Mikolaj Kieres
    Mikolaj Kieres almost 6 years
    To everyone who ended up here and is genering icons with photoshop (or any other graphic design tool). Don't use CMYK mode! Use RGB when creating/exporting an icon - it helped in my case
  • Pradip Tilala
    Pradip Tilala about 5 years
    Added the icon as suggested by the "Oblivionkey3" but after adding <meta-data android:name="com.google.firebase.messaging.default_notifica‌​tion_color" android:resource="@color/custom_color" /> icon color set to custom. Before this icon color was grey on Andriod Oreo notification (Status bar was icon being displayed in white).
  • mukesh.kumar
    mukesh.kumar almost 5 years
    what is custom_color ? and how to set it?
  • Raul Pinto
    Raul Pinto over 4 years
  • radu_paun
    radu_paun over 4 years
    @mukesh.kumar, custom_color is any color you want to use. you just need to declare it in colors file
  • Michael Alan Huff
    Michael Alan Huff over 3 years
    God bless you Advice-Dog. This was the answer I needed tonight.
  • Bip901
    Bip901 over 3 years
    I used this tool, my image is a pure white transparent PNG and I made sure it's in the RGB color space, but SetColor still doesn't work. What am I missing?
  • Bip901
    Bip901 over 3 years
    To anyone still stuck, try downscaling the image. A 72x72 icon didn't work for me, but 48x48 did.
  • Johann
    Johann over 2 years
    They should have called "setColor" "setIconColor" instead and it woud have been obvious.