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)
Related
I am using the below snippet to generate notifications in my android app.
private void sendNotification(String contentText, String message) {
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.putExtra("clear","clear");
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent piResult = PendingIntent.getActivity(this, 0, resultIntent,0);
NotificationCompat.Builder builder=new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setColor(ContextCompat.getColor(getApplicationContext(),R.color.red))
.setContentTitle("title")
.setContentText(message)
.setAutoCancel(true)
.setLargeIcon(BitmapFactory.decodeResource(getResources()
,R.drawable.notification))
.setContentIntent(piResult);
NotificationCompat.InboxStyle notification = new NotificationCompat.InboxStyle(builder);
int i;
for(i=0; i<messageList.size();i++){
notification.addLine(messageList.get(i));
}
notification.setBigContentTitle("title");
notification.setSummaryText(contentText);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID,notification.build());
}
It works in android 5 and 6 but for android nougat it is not working
Following the docs from the Android API:
Status bar icons are composed simply of white pixels on a transparent backdrop, with alpha blending used for smooth edges and internal texture where appropriate.
Your whole image but the transparent parts is going to be converted to white (being originally white or having colors).
One solution is to create a silhouette icon with color, by this way you can use the same image in all Android APIs. One example could be this icon:
In lower versions of Android you would see the black face, in the last versions you will see the same face but with white color. That's because of the transparent parts (it seems SO removes the transparency, you can get the original from here) of the image.
From android version lollpop onwards, they have made the changes for the notifications. When you are specifing the small icon, it should be of specific size as mentioned in this link.
The important thing is the image should be transparent and contains only white color.
You can check this question to get the answer
According to this blog here
It says that
You’ll note that the icons are not present in the new notifications; instead more room is provided for the labels themselves in the constrained space of the notification shade. However, the notification action icons are still required and continue to be used on older versions of Android and on devices such as Android Wear.
If you’ve been building your notification with NotificationCompat.Builder and the standard styles available to you there, you’ll get the new look and feel by default with no code changes required.
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.
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.
I am creating an application, I am able to display notification properly, but small icon is not getting displayed as I have mentioned it in the drawable folder, The icon is getting masked with white color. Can any one help me, how can I get the icon to display properly.
Below is my notification code:
nb = new NotificationCompat.Builder(context)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setSmallIcon(icon)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.micon_notification))
.setWhen(when)
.setContentIntent(contentIntent)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setTicker(tickerText)
.setColor(Color.RED);
The icon mentioned in drawable is as shown below:
[1]: http://i.stack.imgur.com/ggYCY.png
That complete red color present inside the image is getting vanished and icon is getting displayed with complete white color. All suggestions are welcome.
This is the code Android uses to display notification icons:
if (entry.targetSdk >= Build.VERSION_CODES.LOLLIPOP) {
entry.icon.setColorFilter(mContext.getResources().getColor(android.R.color.white));
} else {
entry.icon.setColorFilter(null);
}
For that you've to make icon like Silhouette and make some section Transparent wherever you wants to add your Colors.
You can add your color using
.setColor(your_color_resource_here)
NOTE : setColor is only available in Lollipop so, you've to check OSVersion
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Notification notification = new Notification.Builder(context)
...
} else {
// Lollipop specific setColor method goes here.
Notification notification = new Notification.Builder(context)
...
notification.setColor(your_color)
...
}
Look at the documentation: http://developer.android.com/design/style/iconography.html
there are words:
"Notification icons must be entirely white. Also, the system may scale
down and/or darken the icons."
I hope it helps!
When using Parse for push notifications our app always displayed the application's launcher icon.
In the latest Android 5.1 version, the icon appears to be blank (a white square).
I tried setting the icon in the meta data:
<meta-data android:name="com.parse.push.notification_icon" android:resource="#drawable/noti_icon"/>
Based on the question here
But nothing seems to work.
Any ideas?
You must use a transparent and white icon under Android Lollipop 5.0 or greater. You can extend ParsePushBroadcastReceiver class and override the two methods to get your notification icon compatible with these Android APIs.
#Override
protected int getSmallIconId(Context context, Intent intent) {
return R.drawable.your_notifiation_icon;
}
#Override
protected Bitmap getLargeIcon(Context context, Intent intent) {
return BitmapFactory.decodeResource(context.getResources(), R.drawable.your_notifiation_icon);
}
Remember to customize your code to support Lollipop and previous APIs.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
return BitmapFactory.decodeResource(context.getResources(), R.drawable.your_notifiation_icon_lollipop);
}
else{
return BitmapFactory.decodeResource(context.getResources(), R.drawable.your_notifiation_icon);
}
It is not related to Parse or push nitification, but just how Android 5.0 handles notification icons.
See this releated question for details:
Notification bar icon turns white in Android 5 Lollipop
Although #Pelanes has the correct answer (and should be accepted), here's what I did. Note that the Parse docs for getSmallIconId state the following:
Retrieves the small icon to be used in a Notification. The default implementation uses the icon specified by com.parse.push.notification_icon meta-data in your AndroidManifest.xml with a fallback to the launcher icon for this package. To conform to Android style guides, it is highly recommended that developers specify an explicit push icon.
So it is not entirely necessary to override the getSmallIconId() and getLargeIcon() methods.
What I did to solve the problem was I just made a copy of my icon, punched transparent "holes" into the icon, and set the com.parse.push.notification_icon meta-data in my manifest to point to this new icon.
For Android 5.0, it is required for your notification icon to be white and transparent, as others have mentioned. So creating the separate icon is necessary. One line in the manifest and one new drawable file is all it takes.
Try this code.
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(largeIcon)
.setContentText(data)
.setContentTitle("Notification from Parse")
.setContentIntent(pendingIntent);