The method setColor(int) is undefined for the type NotificationCompat.Builder

2.2k Views Asked by At

I'm working on a simple project which sends push notifications.

I wanted to design my notification a bit by setting colors, icons etc. According to Android Developer site, there's a method to NotficationCompat.Builder class called setColor and setCategory, but both give me a compilation error.

I've updated the android.support.v4.... to the latest one, any more ideas? Thanks in advance

EDIT: Here's a code snippet of the issue:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setDefaults(defaults)
            .setSmallIcon(R.drawable.icon)
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.alert))
            .setColor(Color.RED) // The error is here!!!
            //.setCategory(Notification.CATEGORY_ALARM)
            .setWhen(System.currentTimeMillis())
            .setSubText("Tap here for more details")
            .setContentTitle("Care@Home Alert")
            .setTicker("Care@Home Alert")
            .setContentIntent(contentIntent)
            .setAutoCancel(true);

The exact error is copied to the question title.

2

There are 2 best solutions below

6
On BEST ANSWER

Updated: The issue is that the support library wasnt updated to the latest version.

0
On

setColor is only available in Lollipop so, you've to check OSVersion

 if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Notification notification = new Notification.Builder(context)}else {
Notification notification = new Notification.Builder(context)

notification.setColor(your_color)

}