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

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



首先在我们的工具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<String, String> 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.运行效果截图:


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值