We can do like below:
Create a new object of notification builder and call setSmallIcon()
using notification builder object like in below code.
Create a method in which we will check on which OS version we are installing our app . If it is below Lolipop i.e API 21 then it will take the normal app icon with background color else it will take the transparent app icon without any background. So the devices using os version >= 21 will set the background color of icon using method setColor()
of Notification builder class.
Sample Code:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setSmallIcon(getNotificationIcon(notificationBuilder));
private int getNotificationIcon(NotificationCompat.Builder notificationBuilder) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int color = 0x008000;
notificationBuilder.setColor(color);
return R.drawable.app_icon_lolipop_above;
}
return R.drawable.app_icon_lolipop_below;
}