Android的通知,大图片不能正常工作不能正常、大图片、通知、工作

2023-09-06 14:03:34 作者:耂资ヘ专情限量版

我创建我的通知是这样的:

 意向意图=新的意图(这一点,OfferNotification.class);
        PendingIntent pIntent = PendingIntent.getActivity(此,0,
                意图,0);
        乌里soundUri = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder mBuilder =新NotificationCompat.Builder(
                本).setSmallIcon(R.drawable.unknown)
                //。setLargeIcon(BitmapFactory.de codeResource(getResources(),R.drawable.unknown))
                .addAction(R.drawable.ic_launcher,D,pIntent)
                .setAutoCancel(真)
                .setContentTitle(「收购建议的+ restaurantName)
                .setContentText(offerDescriptoin).setSound(soundUri);
        //创建一个明确的意图在你的应用程序的活动
        意图resultIntent =新的意图(这一点,OfferNotification.class);
        resultIntent.putExtra(offerID,offer.getID());
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(本);

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

当我使用小图标,它工作得很好,但是当我使用大图标,我可以发nofitication的声音,但该通知本身没有出现任何帮助pelase?

解决方案

Android的图标(和其他UI元素,如拖动长度)在DP测量。一个DP是设备/密度无关像素。 1 DP相当于1像素160 dpi的屏幕上。但转换到其他屏幕密度,你需要通过一个密度因子相乘。因此,通常建议多幅图像,提供大部分的图标。

例如,在状态栏中使用的通知图标被指定为24x24的DP,有1 DP保证金(因此实际的图标只占用一个22x22 DP光广场,尽管一些机管局会渗进了1 DP保证金/ safeframe)。要转换24 DP实际像素大小,这些粗略计算用于:

 显示密度DP *单位规模= PX装置
LDPI〜120 dpi的24x24的DP * 0.75 = 18×18像素
MDPI〜160 dpi的24x24的DP * 1.0 = 24×24像素
华电国际〜240 dpi的24x24的DP * 1.5 = 36X36像素
xhdpi〜320 dpi的24x24的DP * 2.0 = 48×48像素
 
开发者再曝安卓7.0官方截图 通知栏变化一目了然

还有称为tvdpi的中间​​显示密度(〜213 dpi)的,它位于MDPI和HDPI之间,并且具有1.33的比例因子,但是这是很少见。什么是Android文档建议是,你遵循一个3:4:6:8比例比最常见的显示密度提供prescaled位图图像(通常是PNG格式)时

我没有看到任何地方,他们指定DP大小在通知中所用的大图标,但在正常的收件箱中查看每个通知的高度为64 DP。因此,这意味着用于显示会有图标/图像最大尺寸:

  LDPI:48×48像素
MDPI:64×64像素
华电国际:96×96像素
xhdpi:128×128像素
 

如果你想知道到底是什么图像尺寸Android的股票图标,你应该能够从Android图标模板包发现,V4.0。

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?

解决方案

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.