在GCM中没有图像

时间:2016-03-14 07:44:34

标签: android google-cloud-messaging

我正在进行GCM自定义,我正在尝试获取图像并希望设置通知,但是当尝试发送通知时它会在我的logcat中给出错误,我能够在我的logcat中正确地获取消息但是图像没有得到

错误

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代码

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

5 个答案:

答案 0 :(得分:2)

您无法使用setSmallIcon()来设置互联网上的图片。 setSmallIcon应该指向资源中的一个图像drawable(例如:R.drawable.my_app_icon)。要从其他地方设置图片,您需要使用setLargeIcon()。此方法接受位图,因此您应将指定的Url中的图像作为位图下载,然后将其设置为setLargeIcon()方法

答案 1 :(得分:1)

您无法从网址获取图片 你试图从它如何获得它?

首先从url下载它,然后给位图通知。尝试以下代码

<强>更新

添加了使用位图生成通知的代码。

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());

答案 2 :(得分:1)

您将图像发送到GCM或只是图像的URL,以便android从后台下载该图像?

如果您使用的是第一名。我认为这是不好的方法。只需发送网址并下载即可。

使用Picasso library更轻松

答案 3 :(得分:1)

这是这些行的标语整数imgs = Integer.parseInt(img); 你传递一个没有任何int值的字符串,所以你得到NumberFormatException

尝试这样做

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());



        }
}

答案 4 :(得分:0)

你可以像这样创建通知

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());