Android GCM 推送通知大图标大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25030710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:01:43  来源:igfitidea点击:

GCM Push Notification Large Icon size

androidandroid-notificationsgoogle-cloud-messaging

提问by Ram G Athreya

Hi Iam implementing Push Notifications in Android using GCM. I am trying to set an image for the notification instead of the default app icon. I am able to achieve this using the following code

嗨,我正在使用 GCM 在 Android 中实现推送通知。我正在尝试为通知设置图像而不是默认应用程序图标。我可以使用以下代码实现这一点

if(extras.getString("src") != null){
            URL url = new URL(extras.getString("src"));
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap large_icon = BitmapFactory.decodeStream(input);
            mBuilder.setLargeIcon(large_icon);
        }

Typically the image will be from the web(jpg, png etc) and not something in the device. The above code works but the image is either too big or too small. I would like to know the optimum size or aspect ratio for the bitmap so that I can supply an appropriate image

通常图像将来自网络(jpg、png 等)而不是设备中的某些内容。上面的代码有效,但图像太大或太小。我想知道位图的最佳尺寸或纵横比,以便我可以提供合适的图像

回答by OscarBudo

I was having the same problem. This is how I solve it:

我遇到了同样的问题。我是这样解决的:

First you need to know the max sizes of the notification icon depending of the device resolution. Searching, I found this:

首先,您需要根据设备分辨率了解通知图标的最大尺寸。搜索,我发现了这个:

  • ldpi: 48x48 px *0.75
  • mdpi: 64x64 px *1.00
  • hdpi: 96x96 px *1.50
  • xhdpi: 128x128 px *2.00
  • xxhdpi: 192x192 px *3.00
  • ldpi:48x48 像素 *0.75
  • mdpi:64x64 像素 *1.00
  • hdpi:96x96 像素 *1.50
  • xhdpi:128x128 像素 *2.00
  • xxhdpi:192x192 像素 *3.00

There are 2 approach:

有2种方法:

  • One is having a set of images in those dimension in the server, and get them depending of your device resolution.
  • The other one is having the larger image in the server and resize it in the app depending of your device resolution.
  • 一个是在服务器中具有这些维度的一组图像,并根据您的设备分辨率获取它们。
  • 另一种是在服务器中使用较大的图像,并根据您的设备分辨率在应用程序中调整其大小。

I will explain to you the second one that I implement.

我将向您解释我实施的第二个。

First for get the Image from an URL I use this:

首先从一个 URL 获取图像,我使用这个:

public Bitmap getBitmapFromURL(String strURL) {
    try {
        URL url = new URL(strURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

Then I need to know the factor for the new image size. I know that in the server I have the xxhdpi image with a factor of *3.00, I use that for get the global factor:

然后我需要知道新图像大小的因素。我知道在服务器中我有一个因子为 *3.00 的 xxhdpi 图像,我用它来获取全局因子:

public static float getImageFactor(Resources r){
      DisplayMetrics metrics = r.getDisplayMetrics();
      float multiplier=metrics.density/3f;
      return multiplier;
  }

Now I have to resize the image size and set the new bitmap in the notification icon:

现在我必须调整图像大小并在通知图标中设置新位图:

Bitmap bmURL=getBitmapFromURL(largeIcon);
float multiplier= getImageFactor(getResources());
bmURL=Bitmap.createScaledBitmap(bmURL, (int)(bmURL.getWidth()*multiplier), (int)(bmURL.getHeight()*multiplier), false);
if(bmURL!=null){
    mBuilder.setLargeIcon(bmURL);
}           

This work for me. I hope you can use it.

这对我有用。我希望你可以使用它。

回答by Ranjit

If I understood your problem perfectly, then the below will help you.

如果我完全理解您的问题,那么以下内容将对您有所帮助。

If you have the image already.. then you can set it like

如果您已经拥有图像..那么您可以将其设置为

  .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_app_sky))
  .setSmallIcon(R.drawable.ic_aaja_icon_red)

The total one:

总一:

 NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setNumber(COUNTER)
     .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_app_sky))
     .setSmallIcon(R.drawable.ic_icon_red)
     .setAutoCancel(true)
     .setContentTitle(pushCount > 1 ? "xxx" + pushCount : title)
     .setContentText(pushCount > 1 ? "yyy" : message)
     .setWhen(when)
     .setContentIntent(PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT))
   //.setDeleteIntent(PendingIntent.getBroadcast(context, 0, new Intent(Intent.ACTION_CLEAR_NOTIFICATION), PendingIntent.FLAG_CANCEL_CURRENT))
     .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
     .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

You can also get help from this tutorial..

您还可以从本教程中获得帮助..

EDIT: To change a bitmap size ..taken from here..

编辑:要更改位图大小..取自这里..

Bitmap bitmap = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
profileImage.setImageBitmap(Bitmap.createScaledBitmap(bitmap , 64, 64, false));

回答by Trevor Mack

The other thing here to know is that the base layouts have margins stated on these images, so if you are trying to mimic the behavior seen by the base layouts in a custom layout make sure to do something similar. Checkout notification_template_icon_group.xml for details.

这里要知道的另一件事是基本布局在这些图像上有边距,因此如果您尝试模仿自定义布局中基本布局所看到的行为,请确保执行类似的操作。查看 notification_template_icon_group.xml 了解详细信息。

Here I have computed the math to pixels for you (64dp - 12dp):

在这里,我为您计算了像素的数学运算(64dp - 12dp):

ldpi 48 - 9 = 39
mdpi 64 - 12 = 52
hdpi 96 - 18 = 78
xhdpi 128 - 24 = 104
xxhdpi 192 - 36 = 156