0

How I can change the icon & largeIcon in my notification? I'm trying this code:

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        Notification notification = builder
            .setSmallIcon(android.R.drawable.stat_notify_more)
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setContentTitle("title")
            .setContentText("text").build();

        notificationManager.notify(666, notification);

This works great, notification was displayed. But the notification are stil the same like application icon, not the "stat_notifi_more" from android drawable, why?

And also the STATUS BAR ICON (notification small icon at the top- left side next to battery, wifi and others) does not shows too!

Can someone help me with this?

Edit: I have lollipop 5.1.1 and android 24 (sdk)

EDIT2:

The notification icon (OK) (96x96px):

enter image description here

Status bar icon (24x24px) (NOT OK, default application icon):

enter image description here

real status bar icon:

enter image description here

Notification source:

// Using RemoteViews to bind custom layouts into Notification
            RemoteViews remoteViews = new RemoteViews(getPackageName(),
                    R.layout.notification_layout);

            // Set Notification Title
            String strtitle = "title";
            // Set Notification Text
            String strtext = "text";

            // Open NotificationView Class on Notification Click
            Intent intent = new Intent(this, MainActivity.class);
            // Send data to NotificationView Class
            intent.putExtra("title", strtitle);
            intent.putExtra("text", strtext);
            // Open NotificationView.java Activity
            PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            // Locate and set the Image into customnotificationtext.xml ImageViews
            remoteViews.setImageViewResource(R.id.imagenotileft, R.drawable.stacionar_xhdpi_green);

            // Locate and set the Text into customnotificationtext.xml TextViews
            remoteViews.setTextViewText(R.id.title, "title");
            remoteViews.setTextViewText(R.id.text, "content");

            NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.wall_black)
                    .setTicker("t")
                    .setContentIntent(pIntent)
                    .setContent(remoteViews);

            // Create Notification Manager
            NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            // Build Notification with Notification Manager
            notificationmanager.notify(0, builder.build());

xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imagenotileft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/imagenotileft" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:layout_toRightOf="@+id/imagenotileft" />
</RelativeLayout>
  • The problem is with the notificationId. You have invoked the devil notification. You will need to exorcise your code now. – suku Oct 07 '16 at 02:22

2 Answers2

1

Android notification will always show the application icon. You will need to create a custom notification layout for showing an icon other than application icon. Here, you can use whatever icon/design you want.

setSmallIcon() sets the notification icon that comes on the status bar and not in the notification tray. The drawable you assign here should be very small in dimensions. Check the expected sizes here. I believe your drawable is bigger than the expected size and hence, it is not able to render it. Hence, you are not able to see it in the status bar.

Edit: The small icon in status bar will always be white, so your red and green color will not be shown. I think you are not able to see the green color is because of the translucent nature of the color. Even if your drawable has solid red or green, it will just appear as a white circle. This SO Question has detailed information about it. Hence, my advice is that you just use default app icon for the small icon and use whatever custom icon you want for the notification in the notification tray.

Community
  • 1
  • 1
suku
  • 10,507
  • 16
  • 75
  • 120
  • Great, custom notification layout works great. Status bar icon are also visible now, BUT status bar icon are default applicaiton icon, do you know why? (status bar icon was disabled in settings...) @suku –  Oct 07 '16 at 09:25
  • I'm using setSmallIcon, just like in question @suku –  Oct 07 '16 at 09:27
  • Oh, I think you need to always set setsmallicon ... At least earlier you has to otherwise notification crashed – suku Oct 07 '16 at 09:38
  • Yes, I set the smallicon, but in status bar are default application icon (not the mine smallicon) and that's wrong, I need to change this icon as well as main icon @suku –  Oct 07 '16 at 09:43
  • It should work...what is the dimension of the drawable? Please add status bar screenshot to the question. – suku Oct 07 '16 at 09:49
  • 24x24px (*.png) @suku –  Oct 07 '16 at 09:52
  • there must be something wrong with your phone or try increasing the size of the icon to 40px*40px...Is it coming as a white square in the status bar? – suku Oct 07 '16 at 10:22
  • question was updated, yep I tried to add a 40x40 icon, but still shows the basic application icon... @suku –  Oct 07 '16 at 10:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/125164/discussion-between-suku-and--). – suku Oct 07 '16 at 10:42
  • But I was try also using just white icon android.R.drawable.arrow_up_float and this also does not working. I need to make it with this red and green dots, because this is the status of the application. Co how can I do it? @suku –  Oct 07 '16 at 10:50
0

If you are using Firebase Cloud Messaging: to change the small icon when app is in the background you can do it from manifest:

        <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/notification_icon"
        android:value="@string/default_notification_channel_id" />
Leo S
  • 159
  • 9