微信公众号开发整理(六)--自定义菜单事件推送

1.自定义菜单功能事件推送:

微信公众号开发整理(六)--自定义菜单事件推送_第1张图片

微信公众号开发整理(六)--自定义菜单事件推送_第2张图片

首先在我们的工具MessageUtil中添加菜单事件推送常量,如上截图(代码):

        public static final String MESSAGE_TEXT = "text";//文本消息
public static final String MESSAGE_IMAGE = "image";//图片消息
public static final String MESSAGE_VOICE = "voice";//语音消息
public static final String MESSAGE_MUSIC = "music";//音乐消息
public static final String MESSAGE_VIDEO = "video";//视频消息
public static final String MESSAGE_LINK = "link";//链接消息
public static final String MESSAGE_LOCATION = "location";//地理位置消息
public static final String MESSAGE_EVENT = "event";//事件推送消息
public static final String MESSAGE_SUBSCRIBE = "subscribe";//关注
public static final String MESSAGE_UNSUBSCRIBE = "unsubscribe";//取消关注
public static final String MESSAGE_CLICK = "CLICK";//菜单点击
public static final String MESSAGE_VIEW = "VIEW";//菜单点击
public static final String MESSAGE_NEWS = "news";//图文消息

public static final String MESSAGE_SCANCODE= "scancode_push";//扫码事件

2.servlet中doPost实现业务逻辑,详情见代码注释:

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doPost");
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
PrintWriter out = resp.getWriter();
try {
Map map = MessageUtil.xmlToMap(req);
String toUserName = map.get("ToUserName");
String fromUserName = map.get("FromUserName");
String msgType = map.get("MsgType");
String content = map.get("Content");
String message = null;
if (MessageUtil.MESSAGE_TEXT.equals(msgType)) {
if ("1".equals(content)) {
message = MessageUtil.initText(toUserName, fromUserName, MessageUtil.firstMenu());
} else if ("2".equals(content)) {
message = MessageUtil.initText(toUserName, fromUserName, MessageUtil.menuText());
}else if ("3".equals(content)) {
message = MessageUtil.initNewsMessage(toUserName, fromUserName);
}else if("4".equals(content)){
message = MessageUtil.initImageMessage(toUserName, fromUserName);
}else if("5".equals(content)){
message = MessageUtil.initMusicMessage(toUserName, fromUserName);
}else if ("?".equals(content) || "?".equals(content)) {
message = MessageUtil.initText(toUserName, fromUserName, MessageUtil.menuText());
} else {
message = "";
}
// 添加关注公众号推送事件
} else if (MessageUtil.MESSAGE_EVENT.equals(msgType)) {
// 判断消息推送事件
String eventType = map.get("Event");
//被关注事件
if (MessageUtil.MESSAGE_SUBSCRIBE.equals(eventType)) {
message = MessageUtil.initText(toUserName, fromUserName, MessageUtil.menuText());
//点击菜单推送事件(这里点击菜单我们实现让他回复菜单)
}else if(MessageUtil.MESSAGE_CLICK.equals(eventType)){
message = MessageUtil.initText(toUserName, fromUserName, MessageUtil.menuText());
//VIEW菜单推送事件(这里点击菜单我们实现让他回复EventKey)
}else if(MessageUtil.MESSAGE_VIEW.equals(eventType)){
String url = map.get("EventKey");
message = MessageUtil.initText(toUserName, fromUserName, url);
//扫码菜单事件的回复
}else if(MessageUtil.MESSAGE_SCANCODE.equals(eventType)){
String key = map.get("EventKey");
message = MessageUtil.initText(toUserName, fromUserName, key);
}
//地理位置菜单事件的回复
}else if(MessageUtil.MESSAGE_LOCATION.equals(msgType)){
String Label = map.get("Label");
message = MessageUtil.initText(toUserName, fromUserName, Label);
}

out.print(message);
} catch (DocumentException e) {
e.printStackTrace();
} finally {
out.close();
}
}

}

微信公众号开发整理(六)--自定义菜单事件推送_第3张图片

3.运行效果截图:

微信公众号开发整理(六)--自定义菜单事件推送_第4张图片

你可能感兴趣的:(微信公众号开发整理(六)--自定义菜单事件推送)