监听微信公众号推送过来的信息

有大佬会做 监听个人微信 收到公众号推送过来的信息,我想做个归集  有大佬提供必将重金酬谢

我们就首先写个类用来判断推送过来的是什么类型的事件消息,然后在做出相应的处理即可

public class MessageUtil {
	public static final String MESSAGE_TEXT = "text";//文本消息
	public static final String MESSAGE_NEWS = "news";//
	public static final String MESSAGE_IMAGE = "image";//图片消息
	public static final String MESSAGE_MUSIC = "music";//
	public static final String MESSAGE_VOICE = "voice";//语音消息
	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_SCANCODE = "scancode_push";
}

从前面我们得知微信发给我们的事件消息与我们反馈给微信的信息都是通过xml来完成数据交互的,所以我们还需准备一个有关xml的工具类

public class MessageFormatXml {
	/**
	 * xml 转 map
	 */
	public static Map<String, String> xmlToMap(HttpServletRequest request)
			throws IOException, DocumentException {
		Map<String, String> map = new HashMap<String, String>();
		SAXReader reader = new SAXReader();
		InputStream ins = request.getInputStream();
		Document doc = reader.read(ins);
		Element root = doc.getRootElement();
		List<Element> list = root.elements();
		for (Element e : list) {
			map.put(e.getName(), e.getText());
		}
		ins.close();
		return map;
	}
	/**
	* 处理下要返回给微信的数据格式
	*/
	public static String initText(String toUserName, String fromUserName,
			String content) {
		TextMessage text = new TextMessage();
		text.setFromUserName(toUserName);
		text.setToUserName(fromUserName);
		text.setMsgType(MessageUtil.MESSAGE_TEXT);
		text.setCreateTime(new Date().getTime());
		text.setContent(content);
		return textMessageToXml(text);
	}
}

接下来我们要开始准备我们处理消息的controller层的代码了,在这里我们要特别注意一定要与我们在公众号后台配置的url路径对上。一定一定一定要对上。
还有一定要把地址在公众号后台白名单里面添加上自己的ip

	@ResponseBody
	@RequestMapping(value = "/xxxx",method =RequestMethod.POST)
	public String getMessage(HttpServletRequest request, HttpServletResponse response) throws Exception{
		Map<String,String> map = new MessageFormat().xmlToMap(request);
		
		String fromUserName = map.get("FromUserName");//这是正在你公众号操作的用户id
		String toUserName = map.get("ToUserName");//发过来的公众号id
		String msgType = map.get("MsgType");//发送的消息类型[比如 文字,图片,语音。。。]
		String content = map.get("Content");//发送的消息内容
		String message = null;	
		//判断发送的类型
		switch (msgType){
			case MessageUtil.MESSAGE_TEXT://文本消息
				message = MessageFormat.initText(toUserName, fromUserName, "你好啊,欢迎你来到XXX号");
				break;
			case MessageUtil.MESSAGE_IMAGE://图片消息
				//具体如何操作看自己需求来即可
				break;
			case MessageUtil.MESSAGE_EVENT://关注事件
				String event=map.get("Event");
				if (MessageUtil.MESSAGE_SUBSCRIBE.equals(event)){
					message = MessageFormat.initText(toUserName, fromUserName, "你好啊,欢迎关注XXX!");
				}
				break;
		}
		return message;
	}