2020年1月7日10:41:51
由于微信关闭了模版消息发送,所有改为订阅消息发送,此篇技术已经过时,请浏览最新的服务通知,新博客地址
https://blog.csdn.net/qq_34706514/article/details/103870208
首先先上官方文档
关键部分代码
/**
* 发送模板消息sendTemplateMessage
* 小程序模板消息,发送服务通知
* @param touser 接收者(用户)的 openid
* @param template_id 所需下发的模板消息的id
* @param page 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
* @param formid 表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id
* @return
*/
public static JSONObject sendTemplateMessage(String touser,String template_id,String page,String formid,Map<String,TemplateData> map){
String accessToken = GetTokenUtil.getAccessToken();
// String accessToken = "14_J1q4pOmeNcQNTZijKctjByU4SNHJ9ge7eWqUPh98bpjRCzg4Inx50WMZpL06epBJ3wzVaTI9iYXDC_-glxbFYr5PQ9JVDncc67Q7NbvLn4NSW-zasZx_hFCgI8LmBGybI3a-FVjLPzyKMxlpAGFdAIAUAZ";
sendTemplateMessage sendTemplateMessage = new sendTemplateMessage();
//拼接数据
sendTemplateMessage.setTouser(touser);
sendTemplateMessage.setTemplate_id(template_id);
sendTemplateMessage.setPage(page);
sendTemplateMessage.setForm_id(formid);
sendTemplateMessage.setData(map);
sendTemplateMessage.setEmphasis_keyword("");
String json = JSONObject.toJSONString(sendTemplateMessage);
log.info("##模版发送JSON数据: "+json);
String ret = UrlUtils.sendPost(Wechat.SEND_TEMPLATE_MESSAGE+accessToken, json);
return JSON.parseObject(ret);
}
/**发送模板消息*/
String SEND_TEMPLATE_MESSAGE = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=";
用到的封装的实体
TemplateData.java
sendTemplateMessage.java
/**
* 发送模板消息
* @Author: liangkesai
* @CreateDate: 2018/9/30 16:21
*/
public class sendTemplateMessage {
private String touser; //接收者(用户)的 openid
private String template_id; //所需下发的模板消息的id
private String page; //点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
private String form_id; //表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id
private Map<String,TemplateData> data; //模板内容,不填则下发空模板
private String emphasis_keyword; //模板需要放大的关键词,不填则默认无放大
get set 省略
}
public class TemplateData {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public TemplateData(String value) {
this.value = value;
}
public TemplateData() {
}
}
发送post请求代码
/**
* 发送post请求 json格式
* @param url
* @param param
* @return
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", "application/json");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!"+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}
测试用main方法
public static void main(String[] args) {
Map<String,TemplateData> map = new HashMap<>();
map.put("keyword1",new TemplateData("339208499"));
map.put("keyword2",new TemplateData("2018年9月30日16:33:44"));
map.put("keyword3",new TemplateData("***总部"));
map.put("keyword4",new TemplateData("*****学院"));
JSONObject js = sendTemplateMessage("o89rs0M0EIzrkiN9Va88mFbQyUdQ", "vAsdSQIUPI47H5K5xQVFrc36hK2zgyxioRC_rUxMF9M", "", "1539830935602",map);
System.out.println(js);
}
有不懂的地方可以留言