Android Push Notifications: Icon not displaying in notification, white square shown instead
Cause: For 5.0 Lollipop "Notification icons must be entirely white".
If we solve the white icon problem by setting target SDK to 20, our app
will not target Android Lollipop, which means that we cannot use
Lollipop-specific features.
Solution for target Sdk 21
If you want to support Lollipop Material Icons, then make transparent icons for Lollipop and the above version. Please refer to the following:
https://design.google.com/icons/
Please look at http://developer.android.com/design/style/iconography.html, and we'll see that the white style is how notifications are meant to be displayed in Android Lollipop.
In Lollipop, Google also suggests that we use a color that will be displayed behind the white notification icon. Refer to the link: https://developer.android.com/about/versions/android-5.0-changes.html
Wherever we want to add Colors
https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setColor(int)
Implementation of Notification Builder for below and above Lollipop OS version would be:
Notification notification = new NotificationCompat.Builder(this);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notification.setSmallIcon(R.drawable.icon_transperent);
notification.setColor(getResources().getColor(R.color.notification_color));
} else {
notification.setSmallIcon(R.drawable.icon);
}
Note: setColor is only available in Lollipop and it only affects the background of the icon.
It will solve your problem completely!!
Notification icon on Android shown as a white square
I think you generated the icons of wrong size.
I re-generated them using the android asset studio and seems to be working at my end.
You can download then using this link:
https://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.type=text&source.text.text=capenergy&source.space.trim=1&source.space.pad=0&name=ic_stat_capenergy
Oreo Notification Icon not displaying
Here i use the size of icon
mipmap-mdpi : 24*24
mipmap-hdpi : 36*36
mipmap-xhdpi : 48*48
mipmap-xxhdpi : 72*72
and I have store it into mipmap named as logo_white.png
so while building a notification you can use this like
notificationBuilder.setSmallIcon(getNotificationIcon())...
notificationBuilder = new NotificationCompat.Builder(this, "default")
.setSmallIcon(icon)
.setContentTitle(getResources().getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(data.get("message")))
.setContentText(data.get("message"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_MAX);
Here is the method
private int getNotificationIcon() {
boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
return useWhiteIcon ? R.mipmap.logo_white : R.mipmap.app_icon;
}
Here app_icon
is normal app icon and logo_white
is white transparent app icon
Related Topics
Httpclient Won't Import in Android Studio
How to Open the "Front Camera" on the Android Platform
How to Read a Text File from the Sd Card in Android
How to Get Onclick Event on Webview in Android
How to Use Jni Bitmap Operations for Helping to Avoid Oom When Using Large Images
Android Studio - How to Increase Allocated Heap Size
How to Make an Alert Dialog Fill 90% of Screen Size
Fcm (Firebase Cloud Messaging) Send to Multiple Devices
Android 4.1: How to Check Notifications Are Disabled for the Application
Android: How to Have Viewpager Wrap_Content
How to Handle Dynamic Json in Retrofit
How to Add a Library Project to a Android Project
Android: Broadcast Receiver for Screen on and Screen Off
How to Sign an Android APK File
Custom Toast on Android: a Simple Example