Android notification displays wrong icon - android

I have a static Array with Icon ids:
public static final int[][] ICON_IDS = { {R.drawable.ic_access_alarm_black_24dp, R.drawable.ic_access_time_black_24dp, R.drawable.ic_account_box_black_24dp, R.drawable.ic_add_black_24dp, R.drawable.ic_android_black_24dp, R.drawable.ic_clear_black_24dp, R.drawable.ic_delete_black_24dp },
{ R.drawable.ic_settings_black_24dp, R.drawable.ic_airplanemode_active_black_24dp, R.drawable.ic_filter_list_black_24dp, R.drawable.ic_account_box_black_24dp, R.drawable.ic_airline_seat_individual_suite_black_24dp, R.drawable.ic_delete_black_open_24dp, R.drawable.ic_delete_black_open_24dp}};
I display all the icons and the user can choose one. The id of the chosen icon is passed to the notification manager. It displays the notification like this:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(notification.getIconId())
.setContentTitle(notification.getTitle());
I have checked with an "test-ImageView" that the id is correct. The ImageView displays the icon the user selected (with setImageResource(notification.getIconId());). But on the notification in the notification bar another icon is displayed or none.
If I use .setLargeIcon() it shows the correct icon again.
.setLargeIcon(BitmapFactory.decodeResource(getResources(), notification.getIconId()))
But only in the big icon. The small icon is empty.
The icons are the material icons imported via vector assets.
What is wrong with this?

Found out why. You can't use xml resources as Notification Icons.

Related

Why Notification does not show the correct smallIcon?

I use below code to display notifications in my Android apps. I can display it but both "small-Icon" and "large-Icon" are incorrect.They are not even launcher-icon.! I don't know Android where does they get from !!!!
My device is xiaomi-mi5s, is it related to my device? although other apps like Gmail show correct notifications.
I tested notifications in API 15,17,26 emulators and everything
NotificationCompat.Builder builder = new android.support.v4.app.NotificationCompat.Builder(context, PRIMARY_CHANNEL)
.setSmallIcon(R.drawable.ic_stat_name)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.setContentIntent(getPendingIntent(context))
.setContentTitle("This is dummy title")
.setContentText("This is dummy body text")
.setSound(getDefaultSoundUri())
.setCategory("My_Category")
.setDeleteIntent(getOnDismissedPendingIntent(context));
NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= 26) {
NotificationChannel chan1 = new NotificationChannel(PRIMARY_CHANNEL,
PRIMARY_CHANNEL, NotificationManager.IMPORTANCE_DEFAULT);
chan1.setLightColor(Color.BLUE);
chan1.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
manager.createNotificationChannel(chan1);
}
manager.notify(12121, builder.build());
First of all, Large icon and Small icon can be different.
setSmallIcon(): sets an icon that should be displayed at status bar when notification was received. If setLargeIcon() was not called in the builder, large icon will use small icon drawable. Icon should have size of 24dp.
setLargeIcon(): sets an icon that should be displayed at notification in notification list, that can be opened by user by vertical dragging of status bar.
!!! Since API 21, Android requires you to use only single-colour icons with transparency for small icons (e.g. filled contour of Android with only white colour and transparent background).
As a Large icon, you can use icon of any colours, there are no restrictions.

Strange "oo" Notification Area icon instead of ic_stat_notify in Android

I am trying to set an alarm notification icon in the Notification
Area doing this:
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_notify)
.setCategory(CATEGORY_SERVICE)
.setContentTitle("Service")
.setContentText("Alarm is ON").build();
I made my icon in Android Studio using File -> New -> Image Asset -> "Notification Icon" then choosing a pre-defined clip-art.
The strage thing is, in the Notification Drawer I get the ic_launcer icon
and in the Notification Area I get the trange "oo" icon you can see on the
left side of the image below.
picture of my Notification Area
Have you every seen this? Do you know how to fix it? The phone on which
I am testing is a Meizu U20, Android 6.

Cannot use yellow with Android Nougat notification's small icon

I've a problem setting the notification small icon to yellow in Android 7.x
I'm using notification.setColor(Color.YELLOW); while building the notification object. It shows that olive(ish) color instead of yellow.
Also tried to use notification.setColor(Color.argb(255,255,255,0)); but no luck, it shows the same olive(ish) color.
This is how it looks like in Android 7.x
This is how it looks like in Android 6.x, which is the correct color
Both images display the same notification with the same code base, but using different Android devices.
I'm using PushWoosh to send/receive push notifications, bellow is the exact code I'm using to create the notification object.
public class NotificationFactory extends AbsNotificationFactory {
#Override
public Notification onGenerateNotification(PushData pushData) {
PushwooshUserdata pushwooshUserdata = GsonUtil.fromJson(pushData.getExtras().getString("u"), PushwooshUserdata.class);
//create notification builder
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getContext());
notificationBuilder.setContentTitle("Header");
notificationBuilder.setContentText("Message");
//set small icon (usually app icon)
notificationBuilder.setSmallIcon(R.drawable.notification_icon);
notificationBuilder.setColor(Color.argb(255,255,255,0));
//set ticket text
notificationBuilder.setTicker(getContentFromHtml(pushData.getTicker()));
//display notification now
notificationBuilder.setWhen(System.currentTimeMillis());
//build the notification
final Notification notification = notificationBuilder.build();
//add sound
addSound(notification, pushData.getSound());
//add vibration
addVibration(notification, pushData.getVibration());
//make it cancelable
addCancel(notification);
//all done!
return notification;
}
#Override
public void onPushReceived(PushData pushData) {
}
#Override
public void onPushHandle(Activity activity) {
}
}
Android is ensuring a minimum contrast ratio between the foreground color and background color.
With the yellow (#ffff35) foreground and a white background, the contrast ratio is only 1.07:1.
The olive foreground (#717d13) has the minimum contrast ratio of 4.5:1.
This is the relevant patch in the Android source: https://android.googlesource.com/platform/frameworks/base.git/+/4ff3b120ff8a788e3afeb266d18caf072f0b8ffb%5E%21/
I calculated the above contrast ratios using http://webaim.org/resources/contrastchecker/.
Try to ensure that UI controls in a notification are also available in an Activity in your app, and you should always start that Activity when users click the notification. To do this, use the setContentIntent() method.
if you've defined color in colors.xml then in your NotificationBuilder add value as .setColor(getResources().getColor(R.color.<YOUR_COLOR>))
Source: NotificationCompat.Builder#setColor(int)

setSmallIcon() dynamically when receiving notification?

This is for a general notification app. I've managed to capture and set the large icon for the notification since it accepts a Bitmap type as a parameter, but setting the small icon is proving to be a lot more tricky. It only accepts an int resource ID. Since I have no idea what application might be sending a notification to my listener, I would have to generate it dynamically. I can get the small icon by listening for incoming notifications and extracting it, but I can't seem to find a way to set it via its resource ID.
I don't think it's possible, but just want to confirm. If so, is there a workaround, or would I have to manually load small icons of specific apps I want to support into my Drawable folder and then use the resource Id from there? That sounds like a hassle seeing that I've already gotten the right icon, but can't load it in! Below is my code:
public class NotificationService extends NotificationListenerService {
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
Bundle extras = sbn.getNotification().extras;
int id1 = extras.getInt(Notification.EXTRA_SMALL_ICON);;
//Building Notifications
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context.getApplicationContext(id1))
.setSmallIcon(R.mipmap.ic_launcher) //This gives an error
.setContentTitle("My Title")
.setContentText("My Text");
NotificationManager notificationManager =
(NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notiId, mBuilder.build());
}
}
Small icon might not load if it exceeds the size prescribed by Android.Generally we can load small icons through images stored in resource folder only.
can get the details regarding icon sizes for push notifications from the following link
GCM Push Notification Large Icon size
Small Icons in push notifications cannot be added dynamically. Large Icons can be added dynamically by running an Async task. Application will crash if there is no small icon. Small Icon is mandatory for PushNotification.

Set status bar icon names

I'm about to publish my first application. I created all the necessary icons, in my case the launcher icons and the status icons (which I only created for my app next versions updates).
But I am wondering where the status icons names are set. I mean, the launcher icon name is set in the Manifest file this way :
android:icon="#drawable/ic_launcher"
so is there a similar place to set the status icon name?
Thanks in advance!
I'm pretty sure you explicitly set the icon when you make a notification. So:
int icon = R.drawable.notification_icon;
CharSequence tickerText = "Your Text";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
This was taken directly from http://developer.android.com/guide/topics/ui/notifiers/notifications.html

Categories

Resources