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.
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!!
In android notification, it's seam their is 3 different icons (see the picture below). I know how to set the large icon (via setlargeicon), however i don't know how to set the both 2 small icons as their is only one procedure setSmallIcon available.
It is the same setSmallIcon for both locations. However, you can also use setColor() to set the background color on the notification - that is how the lower icon gets the blue background.
I am working on an app that sends notifications using the NotificationManager. I set up an Android Wear emulator and have my android phone connected to the emulator. The notifications my app generates appear on the Android Wear emulator, but the icons and color used are not what I'd expect.
The notification uses 1 of 3 icons as the notification icon, and sets a different LED color for each of the 3 cases. The notification I see on the watch emulator uses the app icon, and not the icon I set in the notification. Also, the background color of the notification is a solid red background, and I'm not sure what's setting it that color.
How can I get my notifications on the watch to match the icon of the notification I set, and how can I change the BG Color?!
Below, the Pink box is the application icon (and not the notification icon I'd expect).
Watch Notification
The notification icons come from the res/drawable folders of the handheld app. Do you have an icon with the same name in the res/drawable-hdpi folder?
http://developer.android.com/training/wearables/notifications/creating.html
Note: The bitmap that you use with setBackground() should have a resolution of 400x400 for non-scrolling backgrounds and 640x400 for backgrounds that support parallax scrolling. Place these bitmap images in the res/drawable-nodpi directory of your handheld app. Place other non-bitmap resources for wearable notifications, such as those used with the setContentIcon() method, in the res/drawable-hdpi directory of your handheld app.
As far I know the colors of the LED are irrelevant for wearable notifications. The background color is selected by default the icon and its priotity, if you set a big icon image it will use this. Since the quality of that big icon image meight be bad there is another call to achieve that. Here is a full example:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle("title")
.setContentText("message")
// replace the drawable if you want something else
.setSmallIcon(R.drawable.ic_launcher);
NotificationCompat.WearableExtender extender =
new NotificationCompat.WearableExtender();
Bitmap bg = BitmapFactory.decodeResource(context.getResources(),
R.drawable.background);
// in the line above you can change the background image
extender.setBackground(bg);
builder.extend(extender);
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(context);
notificationManager.notify(42, builder.build());
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.
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).