piotrek1543 piotrek1543 - 3 months ago 24
Android Question

Notification - set large icon on pre Lollipop Android version

I had been already working on app with avalanche message notifications and I'd faced a problem with notification large icon size on older than Lollipop Android version driven devices.

On Lollipop and Marshmallow devices it looks fine:



but when I had opened an app on my Android 4.4 emulator, I found this:



The image wasn't scaled, fitted, just cropped in the middle. It definitely looked not good.


NOTE: To make it clear, that this large notification icons is
downloaded from server together with message date and content. It
looks like this:




My solution



After some hours of work in my avalanche notification class I added two methods, which run only if Android pre-lollipop version is detected:


  • the first one changes icon shape from rectangle to square and adds white background

  • the second one changes icon dimensions to 96x96



After changes, my notification icon on the same Kitkat emulator (Moto X 2013) looks much better:



Question



Could you tell me if there is a much simpler way to deal with this problem? If not, which icon sizes I need to support, besides 96x96?

Thanks in advance

Answer

If i understood correctly, you want to have the notification icon to look good both on lollipop devices and pre lollipop, in my experience what i did to ensure a good look in both SDK's was this:

  1. For the notification large icon i use resources with these size's : 96x96 (hdpi) 64x64 (mdpi) 128x128 (xhdpi) 192x192 (xxhdpi) 256x256 (xxxhdpi)

  2. I use this code to configure the notification style:

    NotificationCompat.BigTextStyle notiStyle = new       NotificationCompat.BigTextStyle();
    notiStyle.setBigContentTitle(title);
    notiStyle.bigText(msg);
    
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setContentTitle(title)
            .setContentText(msg)
            .setColor(coloresOnboarding.getColor())
            .setCategory(NotificationCompat.CATEGORY_MESSAGE)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
            .setSmallIcon(R.mipmap.ic_launchersmall)
            .setAutoCancel(true)
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcherlarge))
            .setStyle(notiStyle);
    Intent resultIntent = new Intent(context, HeadAppMain.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(pendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    int mId=1;
    mBuilder.setAutoCancel(true);
    

i hope this help you.

Comments