简体   繁体   中英

notification status bar icon doesn't change to white

As the title say when i launch i notify the small icon on the status bar doesnt change color to white and is almost invisible:

在此输入图像描述

在此输入图像描述

Notification n  = new Notification.Builder(this)
        .setContentTitle("title")
        .setContentText("lorem ipsum dolor sit amet")
        .setSmallIcon((R.drawable.logo_ntf))
        .setLargeIcon(icon)
        .setAutoCancel(true)
        //.addAction(R.drawable.transparent, null, null)
        .build();

After ensuring transparency of your drawable file....

Try adding the following to the Notification.Builder pipeline:

.setColor(color);

color should be a resource int value, referring to a colour, like so:

int color = getResources().getColor(R.color.notification_color);

The non-deprecated way:

int color = ContextCompat.getColor(context, R.color.notification_color);

Source: setColor documentation

For Android 5+, you have to create a transparent icon for notification small icon.

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    notification.setSmallIcon(R.drawable.icon_transperent);
    notification.setColor(getResources().getColor(R.color.notification_color));
} else { 
    notification.setSmallIcon(R.drawable.icon);
}

Check this for more

Cause: For 5.0 Lollipop "Notification icons must be entirely white".

If we solve white icon problem by setting target SDK to 20, our app will not target Android Lollipop, which means that we cannot use Lollipop-specific features.

Implementation of Notification Builder for below and above Lollipop OS version would be:

Notification notification = new NotificationCompat.Builder(this);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    notification.setSmallIcon(R.drawable.icon_transperent);
    notification.setColor(getResources().getColor(R.color.notification_color));
} else { 
    notification.setSmallIcon(R.drawable.icon);
} 

reference link and also read documentation 5.0 Behavior Changes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM