【发布时间】:2015-11-05 21:35:17
【问题描述】:
从两天开始,我就在努力做到这一点。但我失败了。请任何人建议我如何在推送通知中获取图像。提前谢谢你。
编写我尝试过的代码:
@SuppressWarnings("deprecation")
private void handleMessage(Context mContext, Intent intent) {
Bitmap remote_picture = null;
long when = System.currentTimeMillis();
int icon = R.drawable.reload_logo;
try {
Bundle gcmData = intent.getExtras();
if(intent.getExtras().getString("message")!=null)
Log.v("TAG_IMAGE", "" + intent.getExtras().getString("message"));
Log.v("TAG_IMAGE", "" + intent.getExtras().getString("imageurl"));
{
NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();
notiStyle.setSummaryText(intent.getExtras().getString("message"));
try {
remote_picture = BitmapFactory.decodeStream((InputStream) new URL(intent.getExtras().getString("imageurl")).getContent());
} catch (IOException e) {
e.printStackTrace();
}
notiStyle.bigPicture(remote_picture);
notificationManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = null;
Intent gotoIntent = new Intent();
gotoIntent.setClassName(mContext, "com.reloadapp.reload.fragments.MainActivity");//Start activity when user taps on notification.
contentIntent = PendingIntent.getActivity(mContext,
(int) (Math.random() * 100), gotoIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext);
Notification notification = mBuilder.setSmallIcon(icon).setTicker("Reload.in").setWhen(0)
.setAutoCancel(true)
.setContentTitle("Reload.in")
.setStyle(new NotificationCompat.BigTextStyle().bigText(intent.getExtras().getString("message")))
.setContentIntent(contentIntent)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setLargeIcon(remote_picture)
.setContentText(intent.getExtras().getString("message"))
.setStyle(notiStyle).build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
count++;
notificationManager.notify(count, notification);//This will generate seperate notification each time server sends.
}
}catch (Throwable e) {
e.printStackTrace();
}
}
我得到小图标和大图标具有相同的图像。但我给的不一样。
结果:
【问题讨论】:
-
请任何人帮助我。
-
请解释一下您已经尝试过的事情?
-
您是否尝试过将包含图像网址的
json String作为消息 并从中进行解析? -
所以你的问题不是'如何显示这样的通知'你想要的只是你将如何获得这样的图像?当然,正如@Boss所说,你可以传递一个包含网址的
JSON.收到推送后,您可以解析它并获取 url,然后下载图片并显示在您想要的任何地方。 -
@kartheekij 你是如何处理通知图片大小的?
标签: android push-notification google-cloud-messaging