无法在Android Nougat通知的小图标中使用黄色

9

我在Android 7.x中设置通知小图标为黄色时出现了问题。

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

我还尝试了使用notification.setColor(Color.argb(255,255,255,0)),但没有成功,它仍然显示橄榄色。

以下是在Android 7.x上的效果:

Android 7.1

以下是在Android 6.x上的正确颜色:

Android 6.x

这两个图片显示的是相同的通知,使用的是相同的代码库,只是在不同的Android设备上显示。

我正在使用PushWoosh发送/接收推送通知,下面是我用于创建通知对象的精确代码:

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

可能更有帮助的是稍微描述一下您如何构建通知。 - Chisko
谢谢@Chisko,我更新了问题并包含了我正在使用的精确代码。 - Ahmed Youssef
2个回答

16

谢谢@eric-fikus。这就是背后的原因。 - Ahmed Youssef
我对颜色进行了一些测试,这些颜色与白色的对比度小于4.5:1,结果总是得到与我选择的不同颜色,并通过计算新颜色的对比度始终高于4.5。 你救了我的一天! - Ahmed Youssef

0

尽量确保通知中的UI控件也在应用程序的Activity中可用,并且当用户点击通知时,您应该始终启动该Activity。要实现这一点,使用setContentIntent()方法。

如果您已经在colors.xml文件中定义了颜色,则在NotificationBuilder中添加值为.setColor(getResources().getColor(R.color.<YOUR_COLOR>))

来源:NotificationCompat.Builder#setColor(int)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接