public AjaxResult subscribeMessage(String openId, String templateId) {
String results = HttpUtils.sendGet("https://api.weixin.qq.com/cgi-bin/token","grant_type=client_credential&appid="+appid+"&secret="+appsecret);
JSONObject tokenJosn = JSON.parseObject(results);
String accessToken = tokenJosn.getString("access_token");
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;
JSONObject data = new JSONObject();
data.put("touser", openId);
data.put("template_id", templateId);
data.put("page", "index");
JSONObject miniprogram = new JSONObject();
miniprogram.put("appid", appid);
data.put("miniprogram", miniprogram);
// 设置消息内容
JSONObject thing1 = new JSONObject();
thing1.put("value", "thing1");
JSONObject thing2 = new JSONObject();
thing2.put("value", "thing2");
JSONObject thing3 = new JSONObject();
thing3.put("value", "thing3");
JSONObject thing4 = new JSONObject();
thing4.put("value", "thing4");
JSONObject thing5 = new JSONObject();
thing5.put("value", "thing5");
JSONObject thing6 = new JSONObject();
thing6.put("value", "thing6");
JSONObject thing7 = new JSONObject();
thing7.put("value", "thing7");
JSONObject thing8 = new JSONObject();
thing8.put("value", "thing8");
JSONObject thing9 = new JSONObject();
thing9.put("value", "thing9");
JSONObject thing10 = new JSONObject();
thing10.put("value", "thing10");
data.put("data", new JSONObject().fluentPut("thing1", thing1)
.fluentPut("thing2", thing2)
.fluentPut("thing3", thing3)
.fluentPut("thing4", thing4)
.fluentPut("thing5", thing5)
.fluentPut("thing6", thing6)
.fluentPut("thing7", thing7)
.fluentPut("thing8", thing8)
.fluentPut("thing9", thing9)
.fluentPut("thing10", thing10));
String result = HttpUtil.post(url, data.toJSONString());
System.out.println("订阅消息结果:" + result);
return AjaxResult.success(result);
}
/**
* 发送订阅消息
* @param templateType
* @return
*/
public AjaxResult sendMessage(String userId,String templateType,String activityId,String content) {
RoomMiniUser result = miniUserService.queryById(userId);
if(result == null){
return AjaxResult.error(401, "授权信息过期,请重新授权");
}
RoomActivity roomActivity = roomActivityService.getById(activityId);
String tokenData= HttpUtils.sendGet("https://api.weixin.qq.com/cgi-bin/token","grant_type=client_credential&appid="+appid+"&secret="+appsecret);
JSONObject tokenJosn = JSON.parseObject(tokenData);
String accessToken = tokenJosn.getString("access_token");
String templateId =null;
JSONObject templateContext =new JSONObject();
try {
List<TemplateParam> templateParams = new ArrayList<>();
templateId="自己的模版id";
templateParams.add(TemplateParam.builder().key("thing9").value(roomActivity.getActivityName()).build());
templateParams.add(TemplateParam.builder().key("name1").value(roomActivity.getPublisherName()).build());
templateParams.add(TemplateParam.builder().key("time8").value(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))).build());
templateParams.add(TemplateParam.builder().key("thing4").value(content).build());
}
Template template = Template.builder()
.template_id(templateId)//模板 ID
.touser(result.getOpenId())//用户 openid
.data(templateParams) //模板参数
.page("")//点击消息跳转页面
.build();
//请求地址
String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;
String res = HttpUtil.post(requestUrl, template.toJSON());
if (res != null && !"".equals(res)) {
JSONObject OpenidJSONO = JSONObject.parseObject(res);
String errorCode = OpenidJSONO.get("errcode").toString();
String errorMessage = OpenidJSONO.get("errmsg").toString();
if ("0".equals(errorCode)) {
return AjaxResult.success();
} else {
return AjaxResult.error("订阅消息发送失败:"+ errorCode + errorMessage);
}
}
}catch (Exception ex){
ex.printStackTrace();
}
return AjaxResult.error("订阅消息发送失败");
}
java+微信小程序发送订阅通知
最新推荐文章于 2025-04-09 08:30:00 发布