收藏
回答

微信公众号接收事件推送 无法返回信息给用户

接收事件推送https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_event_pushes.html


@PostMapping
public String handleMessage(@RequestBody String requestBody) {
    try {
        // 解析XML
        Map<String, String> msgMap = parseXml(requestBody);

        String msgType = msgMap.get("MsgType");
        String event = msgMap.get("Event");
        String openId = msgMap.get("FromUserName");

        // 处理关注事件
        if ("event".equals(msgType) && "subscribe".equals(event)) {
            // 新用户关注处理
            WxPayProperties properties = wxPayConfiguration.getWxPayProperties();
            return String.format(
                    "<xml>" +
                            "<ToUserName><![CDATA[%s]]></ToUserName>" +
                            "<FromUserName><![CDATA[%s]]></FromUserName>" +
                            "<CreateTime>%s</CreateTime>" +
                            "<MsgType><![CDATA[text]]></MsgType>" +
                            "<Content><![CDATA[%s]]></Content>" +
                            "</xml>",
                    openId,properties.getAppId(),System.currentTimeMillis() / 1000, "感谢关注!请<a href='https://xxxxx.com/api/wechat.html'>点击授权</a>"
            );
        }

        return "success";
    } catch (Exception e) {
        e.printStackTrace();
        return "error";
    }
} 用户关注后能接受到信息
 但是无法返回消息
到用户 是不是还需要设置什么地方
回答关注问题邀请回答
收藏

1 个回答

  • showms
    showms
    1天前

    FromUserName要传公众号的原始ID

    1天前
    有用 1
    回复 1
    • zhang
      zhang
      1天前
      谢谢
      1天前
      回复
登录 后发表内容