user4256388
user4256388

Reputation:

Android - Notification icon will not change

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>

Upvotes: 0

Views: 4191

Answers (2)

Leo S
Leo S

Reputation: 159

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" />

Upvotes: 0

suku
suku

Reputation: 10929

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.

Upvotes: 1

Related Questions