Ramya Roy
Ramya Roy

Reputation: 247

How can I show an image from link in android push notification?

I have to show an image from demo link

This is the data payload data

{"registration_ids":["","","","","","","","e132domchYE:APA91bEyVcuR8Qpl46
jNfs0CTg4gizFqUDze06wzdzaCsHOI0KwRqcV-Ml5ZlkWJF0b44pCJMWJT0Uk7ApvPoh8pOOtRO0xQWlnj9
Sa8Aii15tSR_U8KB4Dr"],"data":{"title":"50%+discount+sale","message":"grab+now",
"image":"http:\/\/demo.ivdisplays.net\/broadcast\/school_admin\/notification
_images\/thumbnail\/thumb_1490003439class routing.png"}}

I have to show the image in the following link in push notification just like an e-commerce app.

Upvotes: 1

Views: 3783

Answers (1)

Mehul Gajjar
Mehul Gajjar

Reputation: 431

try this, will work

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this);

Bitmap bitmap = getBitmapFromURL(url);

                            NotificationCompat.BigPictureStyle s = new NotificationCompat.BigPictureStyle().bigPicture(bitmap);
                            s.setSummaryText("Image");
                            nBuilder.setStyle(s);
                          nBuilder.setSmallIcon(R.mipmap.ic_launcher);

Create Method getBitmapFromURL

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;
    }
}

mNotificationManager.notify(0, nBuilder.build());

Upvotes: 4

Related Questions