5

我正在创建这样的通知:

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

当我使用小图标时,它工作得很好,但是当我使用大图标时,我可以发出通知的声音,但通知本身没有出现,有什么帮助吗?

4

3 回答 3

7

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.

于 2013-06-08T08:10:33.433 回答
3

我有同样的问题,因为我误解了对 SetSmallIcon 和 SetLargeIcon 的调用。您必须指定一个小图标,否则不会显示通知。大图标是可选的,如果不设置,则使用小图标。

于 2015-07-16T09:11:35.717 回答
1

我认为您应该在 Builder 中请求位图之前对其进行解码,如下所示:

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

它可能没有正确或及时解码。它也将在这里消除一个未知数。

于 2013-06-08T07:39:15.247 回答