通过微信公众平台获取所需配置
获取APPID、secret以及订阅消息的模板ID,模板可以选择微信提供的,也可以自己申请自己需求对应模板。
实现步骤
一、用户授权订阅消息
可直接参照官方文档小程序订阅消息前端配置即可
二、订阅消息推送
1.获取AccessToken
String url = "https://api.weixin.qq.com/cgi-bin/token?" +
"appid=" + "你的小程序的APPID" + "&secret=" + "你的小程序的secret" + "&grant_type=client_credential";
PrintWriter out = null;
BufferedReader in = null;
String line;
StringBuilder stringBuffer = new StringBuilder();
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性 设置请求格式
//设置返回类型
conn.setRequestProperty("contentType", "text/plain");
//设置请求类型
conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
//设置超时时间
conn.setConnectTimeout(1000);
conn.setReadTimeout(1000);
conn.setDoOutput(true);
conn.connect();
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应 设置接收格式
in = new BufferedReader(
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
while ((line = in.readLine()) != null) {
stringBuffer.append(line);
}
JSONObject jsonObject = JSONUtil.parseObj(stringBuffer.toString());
return jsonObject.getStr("access_token");
} catch (Exception e) {
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return null;
2.发送订阅模板消息
需要注意的是不同模板对应的参数是不一样的例如:
此时对应发送的微信的数据格式应该是
建议JsonObject保存字段,具体参数与返回可参照官方文档发送订阅消息
包装参数的代码如下:
JSONObject send = new JSONObject();
JSONObject tip = new JSONObject();
JSONObject code = new JSONObject();
JSONObject goods = new JSONObject();
JSONObject deviceInfo = new JSONObject();
JSONObject deviceName = new JSONObject();
StringBuilder goodsStr = new StringBuilder();
//下面的key是固定的就是"value"自己看接口api然后下面的val值就是发送的内容
tip.set("value", "xxx");
code.set("value", "xxx");
goods.set("value", "xxx");
deviceInfo.set("value", "xxx");
deviceName.set("value", "xxx");
//自己看自己小程序中生成的模板参数然后然后替换我写的key值
send.set("thing11", tip);
send.set("character_string4", code);
send.set("thing5", goods);
send.set("thing7", deviceInfo);
send.set("thing2", deviceName);
wxSubMessageVo.setData(send);
wxSubMessageVo.setTouser("openid");
//发送订阅消息
wxSubMessageUtils.sendTakeOrderMassage(wxSubMessageVo);
发送订阅消息的代码如下:
public void sendTakeOrderMassage(WxSubMessageVo subMessageVo) {
String url = SEND_INFO_URL + getAccessToken();
//拼接推送的模版
//用户的openId
subMessageVo.setTouser("openid");
//订阅消息模板id
subMessageVo.setTemplate_id("模板ID");
subMessageVo.setPage("pages");
subMessageVo.setMiniprogram_state("developer");
JSONObject jsonObject = new JSONObject();
jsonObject.set("touser", subMessageVo.getTouser());
jsonObject.set("template_id", subMessageVo.getTemplate_id());
jsonObject.set("page", subMessageVo.getPage());
jsonObject.set("data", subMessageVo.getData());
String post = HttpUtil.post(url, JSONUtil.toJsonStr(jsonObject));
log.info(post);
}
WxSubMessageVo 实体类如下:
public class WxSubMessageVo {
@ApiModelProperty("用户openId")
private String touser;
@ApiModelProperty("消息模板Id")
private String template_id;
@ApiModelProperty("用户点击消息后跳转到小程序指定的页面路径")
private String page;
@ApiModelProperty("小程序token")
private String access_token;
@ApiModelProperty("跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版")
private String miniprogram_state = "developer";
@ApiModelProperty("语言默认 zh_CN")
private String lang = "zh_CN";
@ApiModelProperty("发送消息")
private JSONObject data;
}