0

I am making GCM customization,i am trying to get image and want to set in notification,but when try to send notification it gives me error in my logcat,i am able to get message properly in my logcat but image not getting

error

03-14 13:12:09.302     660-1462/com. D/MyGcmListenerService﹕ Message: test test
03-14 13:12:09.332     660-1462/com. E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #3
    java.lang.NumberFormatException: Invalid int: "http://myimagepath/UploadImage/Productmaster/155199712TD2420.jpg"
            at java.lang.Integer.invalidInt(Integer.java:138)
            at java.lang.Integer.parse(Integer.java:375)
            at java.lang.Integer.parseInt(Integer.java:366)
            at java.lang.Integer.parseInt(Integer.java:332)
            at com.webpro.techworld.activity.MyGcmListenerService.sendNotification(MyGcmListenerService.java:87)
            at com..activity.MyGcmListenerService.onMessageReceived(MyGcmListenerService.java:71)
            at com.google.android.gms.gcm.GcmListenerService.zzq(Unknown Source)
            at com.google.android.gms.gcm.GcmListenerService.zzp(Unknown Source)
            at com.google.android.gms.gcm.GcmListenerService.zzo(Unknown Source)
            at com.google.android.gms.gcm.GcmListenerService.zza(Unknown Source)
            at com.google.android.gms.gcm.GcmListenerService$1.run(Unknown Source)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)

MYjava code

    public class MyGcmListenerService extends GcmListenerService {

    private static final String TAG = "MyGcmListenerService";

    /**
     * Called when message is received.
     *
     * @param from SenderID of the sender.
     * @param data Data bundle containing message data as key/value pairs.
     *             For Set of keys use data.keySet().
     */
    // [START receive_message]
    @Override
    public void onMessageReceived(String from, Bundle data) {
        String message = data.getString("message");
        String img = data.getString("img");
        Log.d(TAG, "From: " + from);
        Log.d(TAG, "Message: " + message);
        Log.d(TAG, "Images: " + img);

        if (from.startsWith("/topics/")) {
            // message received from some topic.
        } else {
            // normal downstream message.
        }

        // [START_EXCLUDE]
        /**
         * Production applications would usually process the message here.
         * Eg: - Syncing with server.
         *     - Store message in local database.
         *     - Update UI.
         */

        /**
         * In some cases it may be useful to show a notification indicating to the user
         * that a message was received.
         */
        sendNotification(message,img);
        // [END_EXCLUDE]
    }
    // [END receive_message]

    /**
     * Create and show a simple notification containing the received GCM message.
     *
     * @param message GCM message received.
     */
    private void sendNotification(String message,String img) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

       // Integer imgs=Integer.parseInt(img);

        Bitmap bitmap = getBitmapFromURL(img);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("New Product Added")
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
        notificationBuilder.setLargeIcon(bitmap);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }

    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;
        }
    }
}
albert
  • 251
  • 5
  • 20

5 Answers5

2

You cannot use setSmallIcon() for setting image from the internet. setSmallIcon should point to one of the image drawables in your resources (Eg: R.drawable.my_app_icon). To set an image from elsewhere you need to use setLargeIcon(). this method accepts a Bitmap so you should download the image in the specified Url as bitmap and then set it to setLargeIcon() method

Much Overflow
  • 3,142
  • 1
  • 23
  • 40
  • i am foolowing this http://stackoverflow.com/questions/16007401/android-use-external-profile-image-in-notification-bar-like-facebook – albert Mar 14 '16 at 08:32
  • What is the problem you are experiencing? please edit your question or ask a new question – Much Overflow Mar 14 '16 at 08:35
  • `http://myimagepath/UploadImage/Productmaster/155199712TD2420.jpg` this url is not a valid url for an image – Much Overflow Mar 14 '16 at 08:37
  • OK. then the method to fetch image is obviously returning null. Try setting a debug pointer where the method returns null, catch the exception and post the stack trace here – Much Overflow Mar 14 '16 at 08:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106199/discussion-between-albert-and-much-overflow). – albert Mar 14 '16 at 08:47
1

You can't get image from url you are trying to get it from int HOW ?

first download it from url and then give bitmap to notification. try below code

Update

Added code for notification generation with bitmap.

Bitmap remote_picture = HTTPWebRequest.getImageForNotification(ImageUrl);

// Create the style object with BigPictureStyle subclass.
NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();
notiStyle.setBigContentTitle(getResources().getString(R.string.app_name));
notiStyle.setSummaryText(Message);

notiStyle.bigPicture(remote_picture);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setColor(AppConfig.getContext().getResources().getColor(R.color.white))
                .setAutoCancel(true)
                .setContentIntent(intent)
                .setContentTitle(getResources().getString(R.string.app_name))
                .setContentText(Message)
                .setDefaults(Notification.DEFAULT_SOUND)
                .setLargeIcon(notificationLargeIconBitmap)
                .setWhen(System.currentTimeMillis())
                .setStyle(notiStyle);

notificationManager.notify(NotificationID, notificationBuilder.build());
Wasim K. Memon
  • 5,979
  • 4
  • 40
  • 55
1

You send an image to GCM or just URL of the image so android download that image from the background?

If you use the number one. I think it's bad method. Just send the url and download it.

Use Picasso library to make it easier

Rainey
  • 119
  • 2
  • 10
1

This is beacuese of these line Integer imgs=Integer.parseInt(img); you pass an string which not have any int value so you get NumberFormatException

try to do this

private void sendNotification(String message,String img) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

      //  Integer imgs=Integer.parseInt(img);

 if (img != null)
                new DownloadImage().execute(img);

    }

 public class DownloadImage extends AsyncTask<String, Context, Bitmap> {
        Bitmap remote_picture = null;

        @Override
        protected Bitmap doInBackground(String... URL) {

            try {
                remote_picture = BitmapFactory.decodeStream(
                        (InputStream) new URL(URL[0]).getContent());
            } catch (IOException e) {
                e.printStackTrace();
            }
            return remote_picture;
        }

        @Override
        protected void onPostExecute(Bitmap result) {

            if (result != null)
               Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(imgs)
                .setContentTitle("New Product Added")
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());



        }
}
shubham goyal
  • 537
  • 3
  • 3
0

you can create notification like this

NotificationCompat.BigPictureStyle notiStyle = new
                        NotificationCompat.BigPictureStyle();
                notiStyle.setBigContentTitle(getTitle(bundle));

                notiStyle.setSummaryText(getMessage(bundle));
                notiStyle.bigPicture(bitMap);


                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                PendingIntent pendingIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis() /* Request code */, intent,
                        PendingIntent.FLAG_ONE_SHOT);
                Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                        .setSmallIcon(getsmallIcon())
                        .setAutoCancel(true)
                        .setStyle(notiStyle)
                        .setContentTitle(getTitle(bundle))
                        .setContentText(getMessage(bundle))
                        .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.app_icon))
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);
                NotificationManager notificationManager =
                        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                notificationManager.notify((int)System.currentTimeMillis(), notificationBuilder.build());
shubham goyal
  • 537
  • 3
  • 3