[英]Large Icon Bitmap appears as white square in notification?
我有這個問題,我從我在通知中使用的URL生成一個Bitmap
。 但是,在我的手機上, Bitmap
顯示為一個小的白色方塊。 我調查了一下,發現很多帖子就像這樣談論它: 圖標沒有顯示在通知中:顯示的是白色方塊
我確信我的Small Icon
通知確實是透明的。 但是,對於Large Icon
,我意識到Large Icon
不能透明,因為它實際上是我從URL生成的Bitmap
。 我如何解決這個問題並確保圖像正確呈現而不是讓Large Icon
顯示為白色方塊? 這是我的嘗試:
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;
}
圖像顯示我的問題:
編輯:Build.gradle文件顯示:
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 {
}
}
try {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(getNotificationIcon());
Bitmap icon = BitmapFactory.decodeResource(CXGcmListenerService.this.getResources(), R.drawable.ic_launcher);
mBuilder.setLargeIcon(icon);
//Define sound URI
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setContentIntent(pendingIntent);
mBuilder.setContentText(msg);
mBuilder.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg));
mBuilder.setContentTitle(getString(R.string.app_name));
mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
mBuilder.setAutoCancel(true);
mBuilder.setSound(soundUri); //This sets the sound to play
Intent intent = new Intent(CXGcmListenerService.this, CXMainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent notificationIntent = PendingIntent.getActivity(this, 0, intent, 0);
mBuilder.setContentIntent(notificationIntent);
NotificationManager notifyManager = (NotificationManager) CXGcmListenerService.this.getSystemService(Context.NOTIFICATION_SERVICE);
NOTIFICATION_ID++;
notifyManager.notify("" + System.currentTimeMillis(), NOTIFICATION_ID, mBuilder.build());
} catch (Resources.NotFoundException e) {
CXLog.e(TAG, "" + e.getLocalizedMessage(), e);
}
請嘗試此操作以獲取通知中的圖標。
您可以為不同的版本使用不同的圖標:
int bigIcon = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? R.drawable.logo_who_is_not_white: R.drawable.logo_who_is_in_whit_tint;
我想你一直在從URL下載圖片嗎? 所以,請放置一個Asynctask從URL下載圖像,然后使用通知構建器顯示通知,如下所示SO帖子: https : //stackoverflow.com/a/24866080/6452886
Lilipop存在這個問題,你只需看看Lolipop之前的其他手機就沒問題了。
請參考以下鏈接
https://material.google.com/style/icons.html
https://developer.android.com/about/versions/android-5.0-changes.html
否則你可以改變已編譯的SDK
Lollipop及后來的Android API級別的通知發生了變化。 要覆蓋此問題,您可以按照以下方式:
方式1:在app模塊的build.gradle中將targetSdkVersion從23更改為19。 這將解決問題。
注意:也許這個解決方案會在你的構建系統中產生問題(我更喜歡你不應該這樣,但你可以用這種方式檢查你的問題)
方式2:使用黑白圖標更改通知圖標。 因為通知構建器不支持棒棒糖API級別的顏色圖標。
在Android Studio中獲取黑白圖標的方法:
這是正常的,因為Lollipop SDK(API 21 - Ver 5.0.1)僅允許這種類型的配色方案
或者你可以從這里生成: http : //romannurik.github.io/AndroidAssetStudio/icons-notification.html
我認為問題在於圖像大小,狀態欄圖標大小如下 -
ldpi - 18 x 18 px
mdpi - 24 x 24 px
hdpi - 36 x 36 px
xhdpi - 48 x 48 px
您還可以查看此開發人員文檔
您需要將接收到的位圖的尺寸更改為上述尺寸,它應該可以工作。
好吧,因為谷歌已經改變了API 21+的通知風格,並建議使用白色通知圖標
雖然您可以在所有其他設備上使用21+及更舊圖標上的剪影圖標,如下所示
private int getNotificationIcon() {
boolean icon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
return icon ? R.drawable.silhouette : R.drawable.ic_launcher;
}
這是實際的通知構建器代碼:
Notification builder = new Notification.Builder(context)
.setSmallIcon(getNotificationIcon())
.build();
參數
argb -The accent color to use
您傳入2,這不是有效的ARGB顏色,因此您的小圖標的背景顏色無法正確顯示。 而是選擇有效的ARGB顏色。
如果您有想要使用的顏色資源,可以使用如下代碼
.setColor(context.getResources().getColor(R.color.notification_color))
另外,請注意Android 5.0更改狀態:
更新或刪除涉及顏色的資產。 系統會忽略操作圖標和主通知圖標中的所有非Alpha通道。 您應該假設這些圖標僅為alpha。 系統以白色繪制通知圖標,以深灰色繪制動作圖標。
您的小圖標應該是完全白色和透明的 - 您可以使用通知圖標生成器等工具生成適當的圖標。
或者你可以嘗試這個:
int icon = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? R.drawable.your_logo_for_Kitkat : R.mipmap.your_logo_for_Lolipop_and_uper_version;
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(icon)
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getData().get("shortDescription"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setColor(Color.RED)
.setStyle(notiStyle)
.setContentIntent(pendingIntent);
對於Url圖像加載:
private class sendNotification extends AsyncTask<String, Void, Bitmap> {
Context ctx;
String message;
public sendNotification(Context context) {
super();
this.ctx = context;
}
@Override
protected Bitmap doInBackground(String... params) {
InputStream in;
message = params[0] + params[1];
try {
URL url = new URL(params[2]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
in = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(in);
return myBitmap;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
try {
NotificationManager notificationManager = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(ctx, NotificationsActivity.class);
intent.putExtra("isFromBadge", false);
Notification notification = new Notification.Builder(ctx)
.setContentTitle(
ctx.getResources().getString(R.string.app_name))
.setContentText(message)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(result).build();
// hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(1, notification);
} catch (Exception e) {
e.printStackTrace();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.