Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NotificationCompat 4.1 SetSmallIcon and SetLargeIcon

I used this simple code to set a Notification in Android 4.1 or higher. It works well, but my problem comes with SmallIcon and LargeIcon. I understand that SmallIcon is shown in the status bar and the LargeIcon is shown in the dropdown list.

NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setTicker("The ticker"); builder.setContentTitle("The title"); builder.setContentText("The text"); builder.setSmallIcon(R.drawable.my_small_icon); Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.my_96px_large_icon); builder.setLargeIcon(bm);        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.notify("direct_tag", NOTIF_ALERTA_ID, builder.build()); 

My problem is:

  1. When the notification is launched, a cropped oversized Small Icon is shown next to "The Ticker" text, instead of showing the original SmallIcon without oversizing it. enter image description here

  2. In the dropdown list I see the LargeIcon on the left, that's good. But I also see the small icon on the right, next to the time of the notification. I don't want to show that. enter image description here

like image 602
Ton Avatar asked Dec 12 '12 19:12

Ton


People also ask

What is NotificationCompat in android?

NotificationCompat.Action. Structure to encapsulate a named action that can be shown as part of this notification.

What is setContentIntent?

setContentIntent(PendingIntent intent) Supply a PendingIntent to be sent when the notification is clicked. Notification.Builder. setContentText(CharSequence text)

What is Channel ID in android notification?

ChannelId is a unique String to identify each NotificationChannel and is used in Notification.

How do I set notification channels on android?

To create a notification channel, follow these steps: Construct a NotificationChannel object with a unique channel ID, a user-visible name, and an importance level. Optionally, specify the description that the user sees in the system settings with setDescription() .

What are the different types of notification settings in Android?

The priority of the notification: minimum, low, default, high, or maximum. On Android 8.0 and higher, the priority is set via a notification channel. The visibility of the notification on the lock screen: public, private, or secret. Category metadata that helps Android classify and filter the notification.

What is the size of notification icon in Lolipop?

Before Lolipop there was no large icon for notifications. Small icon should be 64x64 and while creating it keep in mind it will be rendered in two colors: white and transparent.

How to view notifications in the notification area?

When a notification is first published, its icon is displayed in the notification area, as shown in the following screenshot: To obtain details about the notification, the user can open the notification drawer (which expands each notification icon to reveal notification content) and perform any actions associated with the notifications.

How to make the notification icon larger than the status bar?

An intent to launch instead of posting the notification to the status bar. NotificationCompat.Builder setLargeIcon(Bitmapicon) Set the large icon that is shown in the ticker and notification.


1 Answers

  1. In my application I provide large (128x128 px) PNG drawable as small icon, and it shows scaled and without cropping. Is your drawable defined in bitmap file or maybe as XML resource? In XML you can specify several aspects of display (e.g. cropping). Double check your XML or use just PNG/JPG.

  2. As Android API documentation on Notification.setSmallIcon() clearly states:

    Set the small icon resource, which will be used to represent the notification in the status bar. The platform template for the expanded view will draw this icon in the left, unless a large icon has also been specified, in which case the small icon will be moved to the right-hand side.

AFAIK there's no way you can override the behaviour, unless you provide your own notification template (via Notification.setContent()

like image 173
Jerzyna Avatar answered Oct 09 '22 03:10

Jerzyna