Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Color Notification Icon

I'm working on an app where I create a notification for the user. I want the icon to appear as white when it's in the status bar, but colored blue when it's being displayed in the drop down notification menu. Here's an example of the same thing being done by the Google Store app.

White notification in status bar:

enter image description here

Colored notification in drop down menu:

enter image description here

How can I replicate this? What properties do I have to set?

Edit: Here's my current code - I made the image all white with a transparent background, so it looks fine in the status bar, but in the notification drop, the image is still the same white color:

private NotificationCompat.Builder getNotificationBuilder() {
        return new NotificationCompat.Builder(mainActivity)
                .setDeleteIntent(deletedPendingIntent)
                .setContentIntent(startChatPendingIntent)
                .setAutoCancel(true)
                .setSmallIcon(R.drawable.skylight_notification)
                .setColor(ContextCompat.getColor(mainActivity, R.color.colorPrimary))
                .setContentTitle(mainActivity.getString(R.string.notification_title))
                .setContentText(mainActivity.getString(R.string.notification_prompt));
    }
like image 561
Oblivionkey3 Avatar asked Aug 25 '17 05:08

Oblivionkey3


People also ask

Can you change the notification bar color on Android?

Step 1: After opening the android studio and creating a new project with an empty activity. Step 2: Navigate to res/values/colors. xml, and add a color that you want to change for the status bar.

How do I change my notification color?

To change the colour, open the app, then go to the app's settings menu to find out which options are available. You can turn LED notifications on or off in the “Settings” menu.


3 Answers

I found the answer to my question here: https://stackoverflow.com/a/44950197/4394594

I don't know entirely what the problem was, but by putting the huge png that I was using for the icon into the this tool https://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.type=image&source.space.trim=1&source.space.pad=0&name=ic_skylight_notification and by placing the generated icons it gave into my mipmap folder, I was able to get the setColor(...) property to work correctly.

like image 183
Oblivionkey3 Avatar answered Oct 17 '22 08:10

Oblivionkey3


For firebase nofitications sent from console you just need to add this in your manifest:

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/white_logo" />

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/custom_color" />

Where white_logo is your app white logo, and custom_color is the color you want to have the icon and text colored.

More details here: https://firebase.google.com/docs/cloud-messaging/android/client

like image 20
radu_paun Avatar answered Oct 17 '22 10:10

radu_paun


Here is what I did for my app ...

private void showNotification(Context context) {
    Log.d(MainActivity.APP_TAG, "Displaying Notification");
    Intent activityIntent = new Intent(context, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
    mBuilder.setSmallIcon(R.drawable.ic_notification);
    mBuilder.setColor(Color.GREEN);
    mBuilder.setContentIntent(pendingIntent);
    mBuilder.setContentTitle("EarthQuakeAlert");
    mBuilder.setContentText("It's been a while you have checked out earthquake data!");
    mBuilder.setDefaults(Notification.DEFAULT_SOUND);
    mBuilder.setAutoCancel(true);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());
}

Sample With Color:

enter image description here

Sample without Color: enter image description here

like image 24
JRG Avatar answered Oct 17 '22 08:10

JRG