I am having this problem where I generate a Bitmap
from a URL that I use in my notification. However, on my phone, the Bitmap
shows up like a small white square. I looked into it and found many posts like so talking about it: Icon not displaying in notification: white square shown instead
and I know for sure my Small Icon
for the notification indeed is transparent. However, for the Large Icon
, I realize that the Large Icon
cannot be transparent because it is actually a Bitmap
that I generate from a URL. How can I get around this then and make sure that the image renders properly instead of having the Large Icon
show up as a white square? Here is my attempt:
NotificationService.java:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setContentTitle(getString(R.string.app_name))
.setContentText(remoteMessage.getNotification().getBody())
.setTicker(remoteMessage.getFrom() + " has responded!")
.setLargeIcon(AndroidUtils.getBitmapFromURL(remoteMessage.getNotification().getIcon()))
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getNotification().getBody()))
.setSmallIcon(R.drawable.ic_tabs_notification_transparent);
AndroidUtils.java:
public static Bitmap getBitmapFromURL(String userId) {
try {
URL imgUrl = new URL("https://graph.facebook.com/" + userId + "/picture?type=large");
InputStream in = (InputStream) imgUrl.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(in);
Bitmap output;
Rect srcRect;
if (bitmap.getWidth() > bitmap.getHeight()) {
output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
srcRect = new Rect((bitmap.getWidth()-bitmap.getHeight())/2, 0, bitmap.getWidth()+(bitmap.getWidth()-bitmap.getHeight())/2, bitmap.getHeight());
} else {
output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getWidth(), Bitmap.Config.ARGB_8888);
srcRect = new Rect(0, (bitmap.getHeight()-bitmap.getWidth())/2, bitmap.getWidth(), bitmap.getHeight()+(bitmap.getHeight()-bitmap.getWidth())/2);
}
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
float r;
if (bitmap.getWidth() > bitmap.getHeight()) {
r = bitmap.getHeight() / 2;
} else {
r = bitmap.getWidth() / 2;
}
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(r, r, r, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, srcRect, rect, paint);
return output;
} catch (IOException e) {
FirebaseCrash.report(e);
return null;
}
Image showing my problem:
EDIT: Build.gradle file showing:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
defaultConfig {
applicationId '<ID>'
multiDexEnabled true
minSdkVersion 21
targetSdkVersion 23
versionCode 12
versionName ".12"
signingConfig signingConfigs.Tabs
}
buildTypes {
release {
minifyEnabled false
shrinkResources false
zipAlignEnabled true
debuggable false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.Tabs
}
debug {
applicationIdSuffix ".debug"
debuggable true
minifyEnabled false
signingConfig signingConfigs.Tabs
}
}
//DatabaseReference stuff
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE-FIREBASE.txt'
exclude 'META-INF/NOTICE'
}
dexOptions {
javaMaxHeapSize "4g"
}
productFlavors {
}
}