Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Large Icon Bitmap appears as white square in notification?

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:

enter image description here

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 {
    }
}
like image 249
user1871869 Avatar asked Oct 12 '16 07:10

user1871869


People also ask

Why are my small notification icons in the status bar white?

Starting with Android 5, the OS forces Small Notification Icons to be all white when your app targets Android API 21+. If you don't make a correct icon, it will most likely be displayed as a bell or solid white icon in the status bar.

What if I don’t set a large notification icon?

If you do not set a large icon, the small icon will be used instead. OneSignal will auto scale large notification icons for you to prevent the icon from being cropped. The recommended size of the large icon is 256x256 pixels. We strongly recommend adding default icons to every Android and Amazon app. Step 1. Generate Icons

What does the white square mean in the android status bar?

This might also include the following icon: A white square, which is displayed as a symbol in the Android status bar. If you want to know what this white square means, this article will help you to find out. First of all, a white square means that the symbol of the corresponding app cannot be loaded.

What is the size of a small icon?

Small Icon. If you prefer to create your own icons, you must make your icons the following size and colors: Type. Size (px) Small Notification Icon (mdpi) 24x24. Small Notification Icon (hdpi) 36x36. Small Notification Icon (xhdpi)


4 Answers

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);
        }

Please try this to get the icon in the notification.

like image 65
Jayamurugan Avatar answered Oct 11 '22 21:10

Jayamurugan


You can use different icon for different version:

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;
like image 32
Md. Al- Rasel Avatar answered Oct 11 '22 21:10

Md. Al- Rasel


I think you have been downloading the image from an URL right?. So, please place an Asynctask to download the image from the url and then use the notification builder to show the notification as like this SO post: https://stackoverflow.com/a/24866080/6452886

like image 1
Madhan Avatar answered Oct 11 '22 20:10

Madhan


Lilipop have this problem exists, You just look at other phone prior to Lolipop that have no problem.

please refer these following links

https://material.google.com/style/icons.html

https://developer.android.com/about/versions/android-5.0-changes.html

otherwise you can change the compiled SDK

like image 1
Jijo Avatar answered Oct 11 '22 21:10

Jijo