java - 奥利奥后通知背景颜色

标签 java android notifications android-8.0-oreo

我正在尝试在 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() 中传递它。

关于java - 奥利奥后通知背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53685355/

相关文章:

java - 检测 Calendar.set() 上的无效日期

c# - 带有计时器的 Xamarin Android UI 没有对更改使用react

c# - 我需要一个网络应用程序中的通知系统

ios - 在没有 AppDelegate 的 iOS 中收到推送通知时如何获取通知?

java - Spring Boot Gradle 插件无法与 Eclipse 一起使用

Java 箭头键 KeyLIstener 逻辑

android - 使用 ibeacon 构建简单的室内定位

android - Bluetooth LE 监听多种特性通知

java - 无法通过 servlet.xml 设置 session 工厂

android - 外部库 jar 不完整