How do I set the notification icon to a specific color?

86 views Asked by At

I am trying to set notification icon which I generated using Android Image Asset. Generated image has just white color and some transparent part.

I pasted here piece of code here. Which doesn't reflect same color what I set in my code. See my code. and please provide solution if possible.

My piece of code looks like this:

Notification inboxStyleNotification = new NotificationCompat.Builder(MyFcmListenerService.this)
                .setContentTitle(title)
                .setContentText(text)
                .setSmallIcon(R.drawable.noti_status_icon)
                .setLargeIcon(anImage)
                .setPriority(Notification.PRIORITY_HIGH)
                .setContentIntent(pIntent)
                .setChannelId(transactions_chnl_id)
                .setCategory(Notification.CATEGORY_MESSAGE)
                .setColor(getResources().getColor(R.color.yellow_light))
                .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                .setStyle(inboxStyle)
                .setAutoCancel(true)
                .build();

yellow_light code is: #FFC107 But when it reflects with R.drawable.noti_status_icon it doesn't reflect with exact yellow_light color.

How to do this?

2

There are 2 answers

7
Muhammad Shah On

first, remove this code part

.setLargeIcon(anImage)
.setStyle(inboxStyle)

then generate a notification icon like this, select an image, set the yellow color there, click next, and finish. it will generate an icon with a yellow color and icon name to noti_status_icon.

enter image description here

enter image description here

then use the below code

Notification inboxStyleNotification = new NotificationCompat.Builder(MyFcmListenerService.this)
            .setContentTitle(title)
            .setContentText(text)
            .setSmallIcon(R.drawable.noti_status_icon)
            .setPriority(Notification.PRIORITY_HIGH)
            .setContentIntent(pIntent)
            .setChannelId(transactions_chnl_id)
            .setCategory(Notification.CATEGORY_MESSAGE)
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .setAutoCancel(true)
            .build();
0
Muhammad Shah On

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

Remember one thing, your notification icon does not contain any background, because the icon with a background not work properly in the notification icon.

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 choose your image ( with large dimension ), download the resource and copy them to your project, I was able to set the setColor(...) property to work correctly.

and finally set the notification icon using .setSmallIcon(R.drawable.ICON_NEW_NAME)