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

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

// 消息预览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 为空,请检查!");
        }
    }

 

posted @ 2017-03-17 17:41  kinc  阅读(3351)  评论(0)    收藏  举报