微信模板消息推送java开发:
一、先查看微信官网定义的模板推送接口需要的参数
image.pngimage.png
接口参数强调:
access_token:根据获取access_token接口使用appid和secret作为参数获取,而不能用授权时返回的access_token,授权时返回的是授权access_token,注意其与普通access_token的区别
image.png
获取普通access_token的接口
image.png
模板id需要在微信官方申请自己企业的推送模板,成功会获得一个模板id
二、准备工作,创建所需要的实体类
1、模板消息实体类
//模板消息实体类
public class TemplateData {
private String value;//模板的值
private String color;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
2、access_token实体类
//access_token实体类
public class AccessToken {
private String access_token;
private String expires_in;
public String getAccess_token() {
return access_token;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
public String getExpires_in() {
return expires_in;
}
public void setExpires_in(String expires_in) {
this.expires_in = expires_in;
}
}
三、消息推送service
1、获取access_token方法
//1、获取access_token方法
public String getAccess_token() {
//获取access_token
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" +
"&appid=" + appId + "&secret=" + secret;
String json = HttpClientUtilCore.doGet(url);
JSONObject jsonObject = JSONObject.parseObject(json);
AccessToken accessToken = JSONObject.toJavaObject(jsonObject, AccessToken.class);
return accessToken.getAccess_token();
}
2、消息推送方法
//用于向微信端发送请求
public String sendMessage(String openId){
//获取access_token
String access_token = getAccess_token();
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send" +
"?access_token=" + access_token;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
// 封装post请求参数
JSONObject json = new JSONObject();
json.put("touser", openId);
json.put("template_id", moduleId);//设置模板id
json.put("url", htmlRedirectUri + "/#/"+flowMessage.getRouterName()+"/"+flowMessage.getFlowId());
JSONObject dataJson = new JSONObject();
TemplateData first = new TemplateData();
first.setValue("流程信息");
first.setColor("#173177");
dataJson.put("first",first);
TemplateData keyword1 = new TemplateData();
keyword1.setValue("流程信息");
keyword1.setColor("#173177");
dataJson.put("keyword1",keyword1);
TemplateData keyword2 = new TemplateData();
keyword2.setValue("流程信息");
keyword2.setColor("#173177");
dataJson.put("keyword2",keyword2);
TemplateData keyword3 = new TemplateData();
keyword3.setValue("流程信息");
keyword3.setColor("#173177");
dataJson.put("keyword3",keyword3);
TemplateData keyword4 = new TemplateData();
keyword4.setValue(flowMessage.getAgoDate());
keyword4.setColor("#173177");
dataJson.put("keyword4",keyword4);
TemplateData keyword5 = new TemplateData();
keyword5.setValue("流程信息");
keyword5.setColor("#173177");
dataJson.put("keyword5",keyword5);
TemplateData remark = new TemplateData();
remark.setValue("流程信息");
remark.setColor("#FF0000");
dataJson.put("remark",remark);
json.put("data", dataJson);
String resultStr = "发送失败";
try {
StringEntity myEntity = new StringEntity(json.toJSONString(), ContentType.APPLICATION_JSON);
// 设置请求参数
httpPost.setEntity(myEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
System.out.println(httpResponse.getStatusLine().getStatusCode());
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// 发送成功
String resutlEntity = EntityUtils.toString(httpResponse.getEntity());
System.out.println(resutlEntity);
resultStr = "发送成功";
return resultStr;
} else {
// 发送失败
return resultStr;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (httpClient != null) {
// 关闭请求
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return resultStr;
}
网友评论