Android 在GCM中,图像未被获取

Android 在GCM中,图像未被获取,android,google-cloud-messaging,Android,Google Cloud Messaging,我正在进行GCM定制,我正在尝试获取图像并希望在通知中设置,但当尝试发送通知时,它在我的logcat中给我错误,我能够在我的logcat中正确获取消息,但图像无法获取 错误 MYjava代码 public class MyGcmListenerService extends GcmListenerService { private static final String TAG = "MyGcmListenerService"; /** * Called w

我正在进行GCM定制,我正在尝试获取图像并希望在通知中设置,但当尝试发送通知时,它在我的logcat中给我错误,我能够在我的logcat中正确获取消息,但图像无法获取

错误

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

您不能使用
setSmallIcon()
从internet设置图像。setSmallIcon应该指向资源中的一个图像可绘制项(例如:R.drawable.my_app_icon)。要从其他地方设置图像,您需要使用
setLargeIcon()
。此方法接受位图,因此您应该将指定Url中的图像作为位图下载,然后将其设置为
setLargeIcon()
method

您不能使用
setmallicon()
从internet设置图像。setSmallIcon应该指向资源中的一个图像可绘制项(例如:R.drawable.my_app_icon)。要从其他地方设置图像,您需要使用
setLargeIcon()
。此方法接受位图,因此您应该将指定Url中的图像作为位图下载,然后将其设置为
setLargeIcon()
method

您无法从Url获取图像 你想从int那里得到它,怎么做

首先从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());

您无法从url获取图像 你想从int那里得到它,怎么做

首先从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());

你向GCM发送一张图片,或者只是发送图片的URL,以便android从后台下载该图片

如果你用一号。我认为这是个糟糕的方法。只需发送url并下载即可


使用可简化操作

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

如果你用一号。我认为这是个糟糕的方法。只需发送url并下载即可


使用使其更简单

这是这些行的信标整数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());



        }
}
private void sendNotification(字符串消息、字符串img){
意向意向=新意向(此,MainActivity.class);
intent.addFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
PendingEvent PendingEvent=PendingEvent.getActivity(此,0/*请求代码*/,意图,
悬挂式帐篷(一杆旗帜);
//整数imgs=Integer.parseInt(img);
如果(img!=null)
新建下载映像().execute(img);
}
公共类DownloadImage扩展异步任务{
位图远程图片=空;
@凌驾
受保护位图doInBackground(字符串…URL){
试一试{
远程图片=位图工厂.decodeStream(
(InputStream)新URL(URL[0]).getContent());
}捕获(IOE异常){
e、 printStackTrace();
}
返回远程图像;
}
@凌驾
受保护的void onPostExecute(位图结果){
如果(结果!=null)
Uri defaultSoundUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_通知);
NotificationCompat.Builder notificationBuilder=新建NotificationCompat.Builder(此)
.setSmallIcon(imgs)
.setContentTitle(“新增产品”)
.setContentText(消息)
.setAutoCancel(真)
.setSound(defaultSoundUri)
.setContentIntent(挂起内容);
通知经理通知经理=
(NotificationManager)getSystemService(上下文通知服务);
notificationManager.notify(通知的0/*ID*/,notificationBuilder.build());
}
}

这是这些行的信标整数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());



        }
}
private void sendNotification(字符串消息、字符串img){
意向意向=新意向(此,MainActivity.class);
intent.addFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
PendingEvent PendingEvent=PendingEvent.getActivity(此,0/*请求代码*/,意图,
悬挂式帐篷(一杆旗帜);
//整数imgs=Integer.parseInt(img);
如果(img!=null)
新建下载映像().execute(img);
}
公共类DownloadImage扩展异步任务{
位图远程图片=空;
@凌驾
受保护位图doInBackground(字符串…URL){
试一试{
远程图片=位图工厂.decodeStream(
(InputStream)新URL(URL[0]).getContent());
}捕获(IOE异常){
e、 printStackTrace();
}
返回远程图像;
}
@凌驾
受保护的void onPostExecute(位图结果){
如果(结果!=null)
Uri defaultSoundUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_通知);
NotificationCompat.Builder notificationBuilder=新建NotificationCompat.Builder(此)
.setSmallIcon(imgs)
.setContentTitle(“新增产品”)
.setContentText(消息)
.setAutoCancel(真)
.setSound(defaultSoundUri)
.setContentIntent(挂起内容);
通知经理通知经理=
(NotificationManager)getSystemService(上下文通知服务);
notificationManager.notify(通知的0/*ID*/,notificationBuilder.build());
}
}

您可以创建如下通知

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

您可以像这样创建通知

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());
我在开玩笑