微信推送模版消息

转载 http://blog.csdn.net/u010513756/article/details/52757125

创建模板消息

进入【微信·公众平台】在【功能】->【添加功能插件】中找到【模板消息】,之后填入相关的信息并通过审核之后即可使用微信模板消息 
这里写图片描述 
先从【模板库】选择一个合适模板,之后添加到【我的模板】中 
这里写图片描述
可以看到模板ID,接下来我们就需要使用这个ID去给指定的用户推送模板消息了

为指定的用户推送模板消息

执行此操作之前需要用户的openid和要用到的模板需要什么参数,这点可以通过模板的【详情】进行查看,我这里的模板需要三个参数first、orderMoneySum、orderProductName 
这里写图片描述 
接下来就需要进行推送信息了,首先我们需要获取用户的openid、模板ID、模板参数数据,将它们以JSON的格式放在String中,之后以POST的方式将这个String提交到https://api.weixin.qq.com/cgi-bin/message/template/send中,代码实现如下

public static String sendMessageByWeiXin(String token,String openId,String name,String parentname) throws ClientProtocolException, IOException{
    String first="恭喜你注册成功";
    String orderMoneySum=name;
    String orderProductName=parentname;
    String date =DateTimeUtil.dateToStr(new Date());
    String wxText = "{\"touser\":\""+openId+"\",\"template_id\":\""+RecommenderModel+"\",\"data\":{\"first\": {\"value\":\""+first+"\",\"color\":\"#000000\"},\"keyword1\": {\"value\":\""+orderMoneySum+"\",\"color\":\"#000000\"},\"keyword2\": {\"value\":\""+orderProductName+"\",\"color\":\"#000000\"},\"remark\":{\"value\":\""+date+"\",\"color\":\"#000000\"}}}";
    return AuthUtil.httpsRequest(sendMessageUrl+"?access_token="+token, "POST", wxText);
}

public static String httpsRequest(String requestUrl, String requestMethod, String outputStr){
        try {
            URL url = new URL(requestUrl);
            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            // 设置请求方式(GET/POST)
            conn.setRequestMethod(requestMethod);
            conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
            // 当outputStr不为null时向输出流写数据
            if (null != outputStr) {
                OutputStream outputStream = conn.getOutputStream();
                // 注意编码格式
                outputStream.write(outputStr.getBytes("UTF-8"));
                outputStream.close();
            }
            // 从输入流读取返回内容
            InputStream inputStream = conn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            StringBuffer buffer = new StringBuffer();
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            // 释放资源
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
            inputStream = null;
            conn.disconnect();
            return buffer.toString();
        } catch (ConnectException ce) {
            System.out.println("连接超时:{}");
        } catch (Exception e) {
            System.out.println("https请求异常:{}");
        }
        return null;
    }

猜你喜欢

转载自blog.csdn.net/chenjiale0102/article/details/78017166