Post-奥利奥通知背景颜色

Post-oreo notification background color

我正在尝试为我在 android Pie 中的通知设置颜色,但是按照 Android Developers 上的信息,我似乎无法让它工作,即通知保持不变白色的。这是我的代码:

public class MainActivity extends AppCompatActivity {

int mColor = Color.CYAN;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void sendNotification(View view) {
    NotificationChannel notificationChannel = new NotificationChannel("1", "1", NotificationManager.IMPORTANCE_DEFAULT);
    notificationChannel.setDescription("Test Notifications");

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(notificationChannel);

    Notification.Builder builder = new Notification.Builder(this, "1");
    builder.setSmallIcon(R.mipmap.ic_launcher_round)
            .setContentText("Test Notification")
            .setColorized(true)
            .setColor(mColor);

    NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
    notificationManagerCompat.notify(1, builder.build());

}

我将不胜感激对此的一些指点,谢谢!

您链接到的方法是这样说的:

For most styles, the coloring will only be applied if the notification is for a foreground service notification. However, for Notification.MediaStyle and Notification.DecoratedMediaCustomViewStyle notifications that have a media session attached there is no such requirement.

除非您为通知使用 MediaStyle 或 DecoratedMediaCustomViewStyle,否则您无法通过对 notify() 的正常调用来设置颜色。

如果您不能使用这些样式中的任何一种,则此通知需要成为服务的一部分,您可以在 startForeground().

中传递它