我正在尝试在 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/