java 后台推送微信模板消息

首先获取token

获取微信token 接口

https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html

/**
 * 获取token
 * @param appId
 * @param secret
 * @return
 */
public String getWxToken(String appId,String secret){
    String accessTokenUrl ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+secret;
    String tokenJson = WeixinUtil.httpRequest(accessTokenUrl, "GET", null);
    JSONObject jsonObject = JSONObject.parseObject(tokenJson);
    String token = jsonObject.getString("access_token");
    System.out.println("获取token -----" +  token);
    return token;
}

 

 //   模板消息参数类

@Data
public class DataEntity {

    private String value;
    private String color;

    public DataEntity(String value ,String color){
        this.value = value;
        this.color = color;
    }

}

 // http 请求微信服务器发送消息

 public static String doGetPost(String apiPath, String type, Map<String,Object> paramMap){

        OutputStreamWriter out = null;
        InputStream is = null;
        String result = null;
        try{
            URL url = new URL(apiPath);// 创建连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestMethod(type) ; // 设置请求方式
            connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
            connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
            connection.connect();

            if(type.equals("POST")){
                out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
                out.append(JSON.toJSONString(paramMap));
                out.flush();
                out.close();
            }


            // 读取响应
            is = connection.getInputStream();
            int length = (int) connection.getContentLength();// 获取长度
            if (length != -1) {
                byte[] data = new byte[length];
                byte[] temp = new byte[512];
                int readLen = 0;
                int destPos = 0;
                while ((readLen = is.read(temp)) > 0) {
                    System.arraycopy(temp, 0, data, destPos, readLen);
                    destPos += readLen;
                }
                result = new String(data, "UTF-8"); // utf-8编码

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return  result;
    }

 // openid: 为你要发送给哪个用户的openid   

发送模板消息接口

public void sendTemplateMessage(String openid,String accessToken ) {
        String templateUrl= "模板消息跳转地址";
        String  templateId = "微信公众号的模板id";
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
        //整体参数map
        Map<String, Object> paramMap = new HashMap<String, Object>();
        //消息主题显示相关map
        Map<String, Object> dataMap = new HashMap<String, Object>();

        dataMap.put("keyword1",new DataEntity(keyword1),"#173177"));
        dataMap.put("keyword2",new DataEntity(keyword2),"#173177"));
        dataMap.put("keyword3",new DataEntity(keyword3),"#173177"));
        dataMap.put("keyword4",new DataEntity(keyword4),"#173177"));
        dataMap.put("keyword5",new DataEntity(keyword5),"#173177"));

        paramMap.put("touser", openid);
        paramMap.put("template_id", templateUrl);
        paramMap.put("url", url);
        paramMap.put("data", dataMap);
        String msgJson = RequestClass.doGetPost(url, "POST", paramMap);
        System.out.println("微信推送消息响应:--------" +msgJson);
      //  Map<String,TemplateData> m = new HashMap<String,TemplateData>();
    }

这样就推送成功拉。 

 

记录一下。 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值