I am using GCM to push notification. In some of the notification i am pushing text and image combination. But i need to show the images only after user click the notification else i should provide a thumbnail of that image should be shown as part of that notification. For that where should i change my code.? In GCM Server or android app side.?
Here is the code what i am doing which will convert Base64 string to bitmap. What i need is thumbnail of this image for notification. If any code which can generate thumbnail of an image should be sufficient.
GCM Listener Service.
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("SERVER_MESSAGE");
String imageBitMap= data.getString("IMAGE_BITMAP");
jsonObject.put("Message", message);
jsonObject.put("ImageBitMap",decodeImage(imageBitMap));
sendNotification(jsonObject);
}
/**
* Decode a base64 string into a Bitmap
*/
private static Bitmap decodeImage(String image_data) {
// Decode the encoded string into largeIcon
Bitmap largeIcon = null;
if ((image_data != null) && (!image_data.equals(""))) {
byte[] decodedImage = Base64.decode(image_data, Base64.DEFAULT);
if (decodedImage != null) {
largeIcon = BitmapFactory.decodeByteArray(decodedImage
, 0
, decodedImage.length);
}
}
return largeIcon;
}
//Notification of Image
private void sendNotification(JSONObject jsonObject) {
String message= null;
Bitmap bitmap=null;
try {
message = jsonObject.getString("Message");
bitmap= (Bitmap) jsonObject.get("ImageBitMap");
} catch (JSONException e) {
e.printStackTrace();
}
Intent intent = new Intent(this, ManualTestActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, REQ_CODE, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(getNotificationIcon())
.setContentTitle("GCM")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bitmap));
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTF_ID, notificationBuilder.build());
}
I have used one more method where image URL is passed and image is getting downloaded in background after download complete i am generating notification.For both the method i need to generate thumbnail of an image.