I'm creating a notification using NotificationCompat.Builder. To set the large icon part of it, I'm using this:
bob = new NotificationCompat.Builder(context)
.setLargeIcon(bm)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher256)
.setContentTitle(context.getResources().getString(R.string.app_name))
.setContentText(message)
.setPriority(Notification.PRIORITY_HIGH);
The smallicon is displaying fine, but on some devices the large icon is zoomed in, and therefore cropped. On other devices (handily, all the ones I can test it on) it works perfectly.
I'm not aware of any restrictions on what size this icon must be. It's coming out of the users contacts list, so I can't control what goes in there, although I suppose I could resize/reformat it if that were the problem.
I'll continue to debug this (focussing on the logic which obtains the bitmap) but thought I'd ask in case I've overlooked something (non) obvious. Perhaps it's possible for people to set contact photos on their devices which aren't suitable for notifications without some pre-processing.
It's not really documented. :(
You can see from the base template layout that the icon is just dunked in an ImageView with android:scaleType="center", so anything bigger than the box (#dimen/notification_large_icon_widthx#dimen/notification_large_icon_height) will just get cropped as it overflows its bounds.
The answer, then, as you can see from dimens.xml, is 64x64dp. You'll want to scale your Bitmaps accordingly before posting the notification.
Related
We have notifications in our app since a long time, which work fine. I have a small, multicoloured .png-icon we use for them, that have worked fine in the past.
In Oreo, the icon is not displayed properly, it is just a grey square. Looking at the drawer on the device, it seems that the system, gmail etc. all now have single-colour icons, so i suspect that it has something to do with that.
However, i can't find any documentation, design guidelines or anything that confirm this, so i am surrendering to a question here...
How can i make my icon display as normal in Oreo? What am i missing?
This is how i create the icon, from what i can see in documentation it should work, and it works pre-Oreo:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(true).setContentTitle("Title").setWhen(when)
.setContentText(message).setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon("icon.png").setContentIntent(contentIntent);
getNotificationManager(context).notify(NOTIFICATION_ID, builder.build());
refer this site
In this site you may see this
You can see that I have set different small icons. Small icon need to
be vector icon on Lollipop and upper Android versions. Otherwise it
will show colored dot only as small icon.
If you have png,jpeg,jpg image file then convert it to svg and import this svg file into android studio using "Vector Asset" option and you are ready to go!!
I need to display a notification with text, image and two buttons.
I'm using NotificationCompat from support library and everything works great until I start to test it on different devices.
Here's the same notification on 3 different devices:
There are some problems here:
Device 1 (Nexus 5, 6.0): message text is missing, image is cropped.
Device 2 (Emulator, 7.0): image is heavily cropped
Device 3 (Xiaomi mi5s, 6.0): message text is missing, action text is black and is almost invisible
Other defects I was able to reproduce: white action icons on white background; image not being displayed (samsung galaxy s8 plus, android 7.0); action buttons not being displayed.
In the end, two biggest problems are: some parts of notification not showing and action icons or action texts having almost the same color as background.
Here's my code to show the notification:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentTitle("Test title");
builder.setContentText("Test notification text description");
builder.setAutoCancel(true);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
builder.setContentIntent(pendingIntentContent);
NotificationCompat.Style style;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && image != null) {
style = new NotificationCompat.BigPictureStyle().bigPicture(image);
builder.setStyle(style);
}
builder.addAction(R.drawable.ic_share_vector, "SHARE", pendingIntentShare);
if(pendingIntentBookMark != null){
builder.addAction(R.drawable.ic_bookmark, "SAVE", pendingIntentBookMark);
}
return builder.build();
I was considering using my own layout, but I can't find a way to understand what is the main color of the device. Some devices use white notifications on black background, some use black notifications on black background and so on. Notification will look alien if it uses a different color.
I understand that I can blame particular devices for this problems, but I still need my notifications to work pretty much anywhere and look more or less consistently (without major glitches such as invisible text or missing elements).
Is there a way to set action buttons background and text color so they will always be visible?
Is there a way to know which colors does device use for its notifications area and for notification backgrounds?
Is there something other than BigPictureStyle that I can use?
I just hope that somebody had the same issues and found any sort of workaround.
I've noticed that Weather Timeline (for instance) displays notifications with two lines in both peek and ambient. I would like to replicate this for my requirements, but I'm not able to do so. I've tried InboxStyle and BigTextStyle to no avail. The icon position is also different, making it possible to display more of the title.
Is this possible somehow, or is it because Weather Timeline is a "wear app"?
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("TestTitle")
.setContentText("TestContextTextLoremIpsum");
The notification card may cover a significant portion of the screen, depending on the system UI style. Your watch face should adapt to these situations by ensuring that users can still tell the time while the notification card is present.
You need to determine the free space above the peek card so you can adapt your watch face, use WatchFaceService.Engine.getPeekCardPosition() method. This provide about its location when it's peeking at the bottom and allowing the watch face to be exposed.
Watch faces should not interfere with system UI elements, as described in Accommodate System UI Elements. If your watch face has a light background or shows information near the bottom of the screen, you may have to configure the size of notification cards or enable background protection.
#Override
public void onCreate(SurfaceHolder holder) {
super.onCreate(holder);
// configure the system UI
setWatchFaceStyle(new WatchFaceStyle.Builder(AnalogWatchFaceService.this)
.setCardPeekMode(WatchFaceStyle.PEEK_MODE_SHORT)
.setBackgroundVisibility(WatchFaceStyle
.BACKGROUND_VISIBILITY_INTERRUPTIVE)
.setShowSystemUiTime(false)
.build());
...
}
The code snippet above configures peeking cards to be a single line tall, the background of a peeking card to show only briefly and only for interruptive notifications, and the system time not to be shown.
I'm trying to add an icon as indicated by number 5 in the image above. I'm using the InboxStyle class and the NotificationCompat.Builder to build it. I've already called setSmallIcon without luck. Any ideas?
Thanks!
As it turns out, this is simply a design change in the new version of Android.
Prior to Android 5.0 Lollipop, setLargeIcon sets the large icon on the left and setSmallIcon sets the small icon on the far right side, as you'd expect.
Starting in Android 5.0, setSmallIcon overlays a small icon on the bottom right corner of the large icon. As far as I can tell, there is no way to make the small icon appear on the far right in Lollipop. Please correct me if I'm wrong.
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:
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.
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.
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.
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()
There is a way around this weird implementation. Instead of using setLargeIcon use this:
Notification notification=notificationBuilder.build()
notification.contentView.setImageViewResource(android.R.id.icon, R.drawable.your_large_icon);
I'd guess that this is the expected behavior.
You should check to see that your small icon follows the UX guidelines for icon size. Small icons are limited to 24x24dp.
The default behavior of an expanded notification is to show both the large icon and the small icon. I'm not sure that there's a way to get rid of the small icon, but why is this important?
In my case, I just set my red icon as the large icon and the setColor to Color.WHITE and set a white icon as my small icon. That way, in the notifications area, my red icon is shown and the white icon is "disapeared".
Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.app_logo);
mBuilder.setContentIntent(resultPendingIntent).setColor(Color.WHITE).setLargeIcon(icon);
In my case I had not placed icon image in all folders (xhdpi,hdpi,mdpi,ldpi).