微信公众号开发巧用消息预览接口实现给用户发送消息


在微信开发中遇到要向用户主动发送微信消息的需求,查阅开发文档并未提供相关接口。搜索也并未发现相关解决办法,灵感突发使用消息预览接口实现了该需求!上图:

// 消息预览URL
private static String message_preview_url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=ACCESS_TOKEN";

 /**
     * 图文消息预览
     *
     * @param touser
     *            接收人openid
     * @param wxArticles
     *            图文集合
     * @throws WexinReqException
     */
    public static void messagePrivate(String accesstoken, String touser, List<WxArticle> wxArticles) throws WexinReqException {

        if (accesstoken != null) {
            String requestUrl = message_preview_url.replace("ACCESS_TOKEN", accesstoken);
            try {
                String mediaId = getMediaId(accesstoken, wxArticles);
                JSONObject obj = new JSONObject();
                JSONObject mpnews = new JSONObject();
                obj.put("touser", touser);
                obj.put("msgtype", "mpnews");
                mpnews.put("media_id", mediaId);
                obj.put("mpnews", mpnews);
                JSONObject result = WxstoreUtils.httpRequest(requestUrl, "POST", obj.toString());
                //System.out.println("微信返回的结果:" + result.toString());
            } catch (Exception e) {

                throw new WexinReqException(e);
            }
        } else {
            throw new WexinReqException("accesstoken 为空,请检查!");
        }
    }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM