How to set the app icon as the notification icon in the notification drawer
P

3

42

As shown in the figure...
I am getting my notification icon(on left to the red colour).
But I need to display the app icon as shown by the black arrow

enter image description here

    public void notify(View view){
    notification.setSmallIcon(R.drawable.ic_stat_name);
    notification.setTicker("Welcome to ****");
    notification.setWhen(System.currentTimeMillis());
    notification.setContentTitle("abcd");
    notification.setContentText("abcd");


    Intent intent = new Intent(this, home.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setContentIntent(pendingIntent);


    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    nm.notify(uniqueID, notification.build());
}
Premature answered 11/12, 2015 at 14:26 Comment(3)
In your notification builder use .setSmallIcon(R.drawable.ic_launcher)Hagiocracy
I have posted my codePremature
@Sathish is incorrect. ic_launcher is not in drawable. Use setSmallIcon(R.mipmap.ic_launcher)Medication
H
85

Try this code at your notification builder :

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
                        R.mipmap.ic_launcher))
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

android.app.NotificationManager notificationManager =
                (android.app.NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

Set Large icon does the trick.Comment below if you have any further info

Hexad answered 11/12, 2015 at 14:29 Comment(8)
Good, Add this line : .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))Hexad
What if, app is not inforegroundInroad
It shows both large icon and small icon, is there any way to remove small iconInroad
I don't see anything wrong with this code but interestingly, this code keeps crashing System UI in Nexus 5X with latest Android O. At first, I thought there was something wrong with the application that I was working with but then I created a new application and again the same thing. This looks like a major Android OS issue.Capelin
not working in 2019. help large icon is still gray circle with white transparent small iconHulsey
Works fine. But the icon is towards the end of the notification. Is there a way to place the icon at the start of notification?Gatling
How to set the large icon on the left side of the text in notification? Currently, I'm getting it on the right side of the textGatling
@Gatling this is the default behavior where the large icon is on the right hand side of the notifications. One can't really change the same but you can try using remote view through which you can create a custom notification layout and populate the same. With this, I think your use case would get catered.Lump
L
101

I know its bit late to answer here and also its already been answered, but I came here looking for an easy fix using firebase notifications. Someone like me visiting here can do the solution by recommended and easy way of firebase notification, which is simply adding meta-data in manifest.

Reference

<!-- Set custom default icon. This is used when no icon is set for incoming notification messages. -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/ic_stat_ic_notification" />
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming notification message. -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/colorAccent" />
Liberate answered 24/7, 2017 at 12:43 Comment(3)
how do you make this dynamic ?Volume
this declaration is also used for firebase default notification handling (when app is in killed state) for dynamic you can use custom in app notifications or modify firebase notifications. Have look at: #37711582Liberate
Helped me to change default icon for Push Notification Display Messages type.Reserved
H
85

Try this code at your notification builder :

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
                        R.mipmap.ic_launcher))
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

android.app.NotificationManager notificationManager =
                (android.app.NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

Set Large icon does the trick.Comment below if you have any further info

Hexad answered 11/12, 2015 at 14:29 Comment(8)
Good, Add this line : .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))Hexad
What if, app is not inforegroundInroad
It shows both large icon and small icon, is there any way to remove small iconInroad
I don't see anything wrong with this code but interestingly, this code keeps crashing System UI in Nexus 5X with latest Android O. At first, I thought there was something wrong with the application that I was working with but then I created a new application and again the same thing. This looks like a major Android OS issue.Capelin
not working in 2019. help large icon is still gray circle with white transparent small iconHulsey
Works fine. But the icon is towards the end of the notification. Is there a way to place the icon at the start of notification?Gatling
How to set the large icon on the left side of the text in notification? Currently, I'm getting it on the right side of the textGatling
@Gatling this is the default behavior where the large icon is on the right hand side of the notifications. One can't really change the same but you can try using remote view through which you can create a custom notification layout and populate the same. With this, I think your use case would get catered.Lump
F
0

Set this parameter while building notification

setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.mipmap.ic_launcher))

Firecrest answered 6/8, 2020 at 11:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.