不能将黄色与 Android Nougat 通知的小图标一起使用

Cannot use yellow with Android Nougat notification's small icon

我在 Android 7.x

中将通知小图标设置为黄色时遇到问题

我在构建通知对象时使用 notification.setColor(Color.YELLOW);。它显示橄榄色(有点)而不是黄色。

也尝试使用 notification.setColor(Color.argb(255,255,255,0)); 但没有成功,它显示出相同的橄榄色。

这是 Android 7.x

中的样子

这是 Android 6.x 中的样子,这是正确的颜色

两张图片使用相同的代码库显示相同的通知,但使用不同的 Android 设备。

我正在使用 PushWoosh send/receive 推送通知,下面是我用来创建通知对象的确切代码。

public class NotificationFactory extends AbsNotificationFactory {
@Override
public Notification onGenerateNotification(PushData pushData) {
    PushwooshUserdata pushwooshUserdata = GsonUtil.fromJson(pushData.getExtras().getString("u"), PushwooshUserdata.class);

    //create notification builder
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getContext());
    notificationBuilder.setContentTitle("Header");
    notificationBuilder.setContentText("Message");

    //set small icon (usually app icon)
    notificationBuilder.setSmallIcon(R.drawable.notification_icon);
    notificationBuilder.setColor(Color.argb(255,255,255,0));

    //set ticket text
    notificationBuilder.setTicker(getContentFromHtml(pushData.getTicker()));

    //display notification now
    notificationBuilder.setWhen(System.currentTimeMillis());

    //build the notification
    final Notification notification = notificationBuilder.build();

    //add sound
    addSound(notification, pushData.getSound());

    //add vibration
    addVibration(notification, pushData.getVibration());

    //make it cancelable
    addCancel(notification);

    //all done!
    return notification;
}

@Override
public void onPushReceived(PushData pushData) {
}

@Override
public void onPushHandle(Activity activity) {
}
}

尝试确保通知中的 UI 控件在 Activity in your app, and you should always start that Activity when users click the notification. To do this, use the setContentIntent() 方法中也可用。

如果您在 colors.xml 中定义了颜色,那么在您的 NotificationBuilder 中将值添加为 .setColor(getResources().getColor(R.color.<YOUR_COLOR>))

来源:NotificationCompat.Builder#setColor(int)

Android保证前景色和背景色的对比度最小。

黄色 (#ffff35) 前景和白色背景,对比度仅为 1.07:1。

橄榄前景 (#717d13) 的最小对比度为 4.5:1。

这是 Android 来源中的相关补丁:https://android.googlesource.com/platform/frameworks/base.git/+/4ff3b120ff8a788e3afeb266d18caf072f0b8ffb%5E%21/

我用http://webaim.org/resources/contrastchecker/计算了上面的对比度。