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 usin
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:
There are 2 approach:
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.