Code Poet
Code Poet

Reputation: 7975

Post-oreo notification background color

I am trying to set a color for my notification in android Pie, but having followed the info on Android Developers, I can't seem to get it to work, i.e. the notification stays white. Here is my code:

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());

}

I would be grateful for some pointers on this, thanks!

Upvotes: 0

Views: 1124

Answers (1)

TheWanderer
TheWanderer

Reputation: 17834

The method you linked to says this:

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.

Unless you use MediaStyle or DecoratedMediaCustomViewStyle for your Notification, you can't set the color with a normal call to notify().

If you can't use either of those styles, then this notification needs to be part of a Service, where you pass it in startForeground().

Upvotes: 1

Related Questions