GCM推送通知大图标大小 [英] GCM Push Notification Large Icon size

查看:242
本文介绍了GCM推送通知大图标大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Iam使用GCM在Android中实现推送通知。我正在尝试为通知设置图片,而不是默认的应用程序图标。我可以使用下面的代码实现这个功能:

  if(extras.getString(src)!= null){
URL url =新的URL(extras.getString(src));
HttpURLConnection连接=(HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
位图large_icon = BitmapFactory.decodeStream(input);
mBuilder.setLargeIcon(large_icon);
}

通常图片将来自网页(jpg,png等),而不是设备中的东西。上面的代码有效,但图像太大或太小。我想知道位图的最佳尺寸或长宽比,这样我就可以提供一个合适的图像了。

一样的问题。这是我如何解决它:



首先,您需要知道通知图标的最大大小,具体取决于设备分辨率。搜索,我发现这个:


  • 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



有两种方法:


  • 一个是在服务器的这些维度中有一组图像,并根据您的设备分辨率获取它们。
  • 另一个是在服务器中放大图像并调整大小在应用中取决于您的设备分辨率。


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



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

  public Bitmap getBitmapFromURL(String strURL){
尝试{
URL url = new URL(strURL);
HttpURLConnection连接=(HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
位图myBitmap = BitmapFactory.decodeStream(input);
返回myBitmap;
} catch(IOException e){
e.printStackTrace();
返回null;


然后我需要知道新图片的因子尺寸。我知道,在服务器中,我有xxhdpi图像的因子是* 3.00,我使用它来获得全局因子:

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

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

 位图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);
}

这对我有用。我希望你能使用它。


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

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

解决方案

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

There are 2 approach:

  • 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:

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:

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.

这篇关于GCM推送通知大图标大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆