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());
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Aditya Suresh
  • 461
  • 1
  • 4
  • 7

3 Answers3

99

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" />
Adam
  • 1,303
  • 11
  • 26
  • 5
    Your answer was the most helpful. Thanks mate. – Curious Mind Mar 09 '18 at 15:19
  • how do you make this dynamic ? – Lena Bru Nov 21 '18 at 10:11
  • 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: https://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase – Adam Nov 21 '18 at 12:07
  • Helped me to change default icon for Push Notification `Display Messages` type. – Mu Sa Nov 22 '18 at 15:02
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

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Manikanta
  • 3,421
  • 4
  • 30
  • 51
  • Good, Add this line : .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher)) – Manikanta Dec 11 '15 at 14:42
  • 1
    What if, app is not inforeground – Mansuu.... May 10 '17 at 12:39
  • It shows both large icon and small icon, is there any way to remove small icon – Mansuu.... May 10 '17 at 12:42
  • 1
    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. – Yogesh Maheshwari Dec 06 '17 at 20:11
  • 4
    not working in 2019. help large icon is still gray circle with white transparent small icon – Sumer Singh Feb 09 '19 at 18:44
  • 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? – Arjun Aug 22 '19 at 06:43
  • 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 text – Arjun Nov 14 '19 at 10:55
  • @Arjun 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. – Vrajesh Hirani Nov 22 '19 at 07:21
0

Set this parameter while building notification

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

Vipul Gupta
  • 51
  • 1
  • 4