微信推送图文消息
看看效果吧,写完后推送成功就是这样的。
发送这个东西分三步
第一步:
调用接口把图片传上去
public static JSONObject addImageMedia(File file){
String url="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token="+TOKEN+"&type=image";
JSONObject jj= HttpClientUtil.httpPostFile(url,file);
return jj;
}
返回结果
{"media_id":"ieIHfih2axTFJEg72bfcAGp","url":"http://mmbiz.qpic.cn/mmbiz_png/AibYR0VkiaicsCMn2INRML3G17ItcBvPRJYzCEhExAG9/0?wx_fmt=png"}
这个返回结果的「media_id」 是我们第二步要用的。
第二步:
创建媒体模板
public static JSONObject addPermanentMediaArticle(){
String PERMANENT_MEDIA_URL = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=%s";
PERMANENT_MEDIA_URL=String.format(PERMANENT_MEDIA_URL,TOKEN);
JSONObject top=new JSONObject();
JSONObject json=new JSONObject();
json.put("title","我是测试图文");
json.put("thumb_media_id","ieIHfih2axTFJEg72bfcAGp");
json.put("author","haier");
json.put("digest","我是描述");
json.put("show_cover_pic",1);
json.put("content","我是内容描述");
json.put("content_source_url","http://baidu.com");
JSONArray arr=new JSONArray();
arr.add(json);
top.put("articles",arr);
JSONObject jj= HttpClientUtil.httpPost(PERMANENT_MEDIA_URL,top,"template");
return jj;
}
返回结果
{"media_id":"ieIHfih2axTFJEg72bfcALl"}
第三步:
发送消息
public static JSONObject sendImageTemplate(){
String MEDIA_PRE_VIEW_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=%s";
MEDIA_PRE_VIEW_URL=String.format(MEDIA_PRE_VIEW_URL,TOKEN);
JSONObject top=new JSONObject();
JSONObject json=new JSONObject();
top.put("touser","OPENID");
top.put("msgtype","mpnews");
json.put("media_id","ieIHfih2axTFJEg72bfcALl");
top.put("mpnews",json);
System.out.println(top.toJSONString());
JSONObject jj= HttpClientUtil.httpPost(MEDIA_PRE_VIEW_URL,top,"template");
return jj;
}
返回结果:
{"errcode":0,"errmsg":"preview success"}
好了,这样就可以推送了。