android notification large image not working

2020-07-16 08:58发布

问题:

I am creating my notification like this:

        Intent intent = new Intent(this, OfferNotification.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0,
                intent, 0);
        Uri soundUri = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.unknown)
                //.setLargeIcon(BitmapFactory.decodeResource( getResources(), R.drawable.unknown))
                .addAction(R.drawable.ic_launcher, "d", pIntent)
                .setAutoCancel(true)
                .setContentTitle("Offer from " + restaurantName)
                .setContentText(offerDescriptoin).setSound(soundUri);
        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(this, OfferNotification.class);
        resultIntent.putExtra("offerID", offer.getID());
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

        stackBuilder.addParentStack(OfferNotification.class);

        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(offer.getID(), mBuilder.build());

when i used small icon, it works very well, but when i use large icon, i can hair the voice of the nofitication but the notification itself doesn't appear, any help pelase?

回答1:

Android icons (and other UI elements, like drag lengths) are measured in dp. A dp is a device/density-independent pixel. 1 dp is equivalent to 1 px on a 160 dpi screen. But to convert to other screen densities, you need to multiply it by a density factor. So it's generally recommended that multiple images are supplied for most icons.

For example, the notification icons used in the status bar are specified as 24x24 dp, with a 1 dp margin (so the actual icon only takes up a 22x22 dp optical square, though some of the AA can bleed into that 1 dp margin/safeframe). To convert 24 dp to actual pixel sizes, these rough calculations are used:

display density  dp units * scale = px units
ldpi  ~120 dpi   24x24 dp * .75   = 18x18 px
mdpi  ~160 dpi   24x24 dp * 1.0   = 24x24 px
hdpi  ~240 dpi   24x24 dp * 1.5   = 36x36 px
xhdpi ~320 dpi   24x24 dp * 2.0   = 48x48 px

There's also an intermediate display density called tvdpi (~213 dpi) that sits between mdpi and hdpi and has a scale factor of 1.33, but this is much less common. What the Android docs recommend is that you follow a 3:4:6:8 scaling ratio when providing prescaled bitmap images (usually PNGs) for the most common display densities.

I don't see anywhere where they specify the dp size for the large icons used in notifications, but the height of each notification in normal inbox view is 64 dp. So that means the max size for icons/images shown there would be:

ldpi:     48x48 px
mdpi:     64x64 px
hdpi:     96x96 px
xhdpi:  128x128 px

If you want to know exactly what image sizes Android's stock icons are, you should be able to find out from the Android Icon Templates Pack, v4.0.



回答2:

I had the same probme cause i missinterpreted the calls for SetSmallIcon and SetLargeIcon. You HAVE to specify a small icon, otherwise the notification is not shown. The big icon is optional, and if not set, the small icon is used.



回答3:

I think you should decode the bitmap before asking for it in the Builder, like this:

 Bitmap bitmap = BitmapFactory.decodeResource( getResources(), R.drawable.unknown);
 Intent intent = new Intent(this, OfferNotification.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0,
                intent, 0);
        Uri soundUri = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.unknown)
                //.setLargeIcon(bitmap)
                .addAction(R.drawable.ic_launcher, "d", pIntent)
                .setAutoCancel(true)
                .setContentTitle("Offer from " + restaurantName)
                .setContentText(offerDescriptoin).setSound(soundUri);

It is perhaps not decoding properly or in time. It also would eliminate one unknown here.