在第二篇的基础上,把订阅响应事件下的,发送文本消息接口替换成发送图文消息的接口。
// 创建图文消息NewsMessage newsMessage = new NewsMessage();newsMessage.setToUserName(fromUserName);newsMessage.setFromUserName(toUserName);newsMessage.setCreateTime(System.currentTimeMillis());newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);newsMessage.setFuncFlag(0);List
其中:NewsMessage
public class NewsMessage extends BaseMessage { // 图文消息个数,限制为10条以内private int ArticleCount; // 多条图文消息信息,默认第一个item为大图private List
}
Article:
public class Article { // 图文消息名称 private String Title;// 图文消息描述 private String Description;// 图片链接,支持JPG、PNG格式,较好的效果为大图640*320,小图80*80,限制图片链接的域名需要与开发者填写的基本资料中的Url一致 private String PicUrl;// 点击图文消息跳转链接 private String Url; public String getTitle() { return Title; } public void setTitle(String title) { Title = title; } public String getDescription() { return null == Description ? "" : Description; } public void setDescription(String description) { Description = description; } public String getPicUrl() { return null == PicUrl ? "" : PicUrl; } public void setPicUrl(String picUrl) { PicUrl = picUrl; } public String getUrl() { return null == Url ? "" : Url; } public void setUrl(String url) { Url = url; } }
MessageUtil:
public class MessageUtil {private static Logger logger = Logger.getLogger(MessageUtil.class);/*** 返回消息类型:文本*/public static final String RESP_MESSAGE_TYPE_TEXT = "text";/*** 返回消息类型:音乐*/public static final String RESP_MESSAGE_TYPE_MUSIC = "music";/*** 返回消息类型:图文*/public static final String RESP_MESSAGE_TYPE_NEWS = "news";/*** 请求消息类型:文本*/public static final String REQ_MESSAGE_TYPE_TEXT = "text";/*** 请求消息类型:图片*/public static final String REQ_MESSAGE_TYPE_IMAGE = "image";/*** 请求消息类型:链接*/public static final String REQ_MESSAGE_TYPE_LINK = "link";/*** 请求消息类型:地理位置*/public static final String REQ_MESSAGE_TYPE_LOCATION = "location";/*** 请求消息类型:音频*/public static final String REQ_MESSAGE_TYPE_VOICE = "voice";/*** 请求消息类型:推送*/public static final String REQ_MESSAGE_TYPE_EVENT = "event";/*** 事件类型:群发消息返回*/public static final String EVENT_TYPE_MASSSENDJOBFINISH = "MASSSENDJOBFINISH";/*** 事件类型:小程序审核通过*/public static final String EVENT_TYPE_SMALLLROUTINE_PASS = "weapp_audit_success";/*** 事件类型:小程序审核通过*/public static final String EVENT_TYPE_SMALLLROUTINE_FAIL = "weapp_audit_fail";/*** 事件类型:subscribe(订阅)*/public static final String EVENT_TYPE_SUBSCRIBE = "subscribe";/*** 事件类型:LOCATION(地理位置推送)*/public static final String EVENT_TYPE_LOCATION = "LOCATION";/*** 事件类型:unsubscribe(取消订阅)*/public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe";/*** 领取卡券事件*/public static final String EVENT_TYPE_USER_GET_CARD="user_get_card";public static final String EVENT_TYPE_COUPONS_GET = "GetCoupons";/*** 卡券审核通过*/public static final String EVENT_TYPE_CARD_PASS_CHECK="card_pass_check";/*** 用户删除卡券*/public static final String EVENT_USER_DEL_CARD="user_del_card";/*** 卡券审核不通过*/public static final String EVENT_TYPE_CARD_NOT_PASS_CHECK="card_not_pass_check";/*** 微信自定义菜单文字点击事件*/public static final String EVENT_TYPE_WECHATMENU_TEXT_CHECK="wechat_menu_text";/*** 微信自定义菜单图文点击事件*/public static final String EVENT_TYPE_WECHATMENU_NEWS_CHECK="wechat_menu_news";/*** */public static final String EVENT_TYPE_WIFI_CONNECT ="WifiConnected";/*** 微信门店审核通过*/public static final String EVENT_TYPE_POI_CHECK_NOTIFY="poi_check_notify";/*** 事件类型:CLICK(自定义菜单点击事件)*/public static final String EVENT_TYPE_CLICK = "CLICK";/** 组装文本消息*/public static String textMsg(String toUserName,String fromUserName,String content){TextMessage text = new TextMessage();text.setFromUserName(toUserName);text.setToUserName(fromUserName);text.setMsgType(REQ_MESSAGE_TYPE_TEXT);text.setCreateTime(new Date().getTime());text.setContent(content);return textMessageToXml(text);}/** 响应订阅事件--回复文本消息*/public static String subscribeForText(String toUserName,String fromUserName){return textMsg(toUserName, fromUserName, "欢迎关注,精彩内容不容错过!!!");}/** 响应取消订阅事件*/public static String unsubscribe(String toUserName,String fromUserName){//TODO 可以进行取关后的其他后续业务处理System.out.println("用户:"+ fromUserName +"取消关注~");return "";}/*** 解析微信发来的请求(XML)* * @param request* @return* @throws Exception*/@SuppressWarnings("unchecked")public static Map
}
效果: