https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/subscribe-message.html
一个模板,一次授权只能发送一次消息,下次发送需要重新授权。
一次授权最多可以有三个模板,每个模板发送一条消息就能发送三条消息。如果有4-6条消息需要发送,则至少需要授权2次。
@Data public class WxMssVo { @ApiModelProperty(value = "用户openid") private String touser; @ApiModelProperty(value = "模版id") private String template_id; @ApiModelProperty(value = "跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版") private String miniprogram_state = "developer"; @ApiModelProperty(value = "进入小程序查看”的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN") private String lang = "zh_CN"; @ApiModelProperty(value = "推送文字") private Map<String, TemplateData> data; }
@Data public class TemplateData { @ApiModelProperty(value = "文字内容,例如keyword1:订单类型,keyword2:下单金额,keyword3:配送地址,keyword4:取件地址,keyword5备注, 依次排下去") private String value; }
String openId = "oulnk5ESr34sHogYLke8m4oy6abc"; String templateId = "fjodangdapgpggjpajgpauufUddjfldjagdajpd"; //拼接推送的模版 WxMssVo wxMssVo = new WxMssVo(); //用户openid wxMssVo.setTouser(openId); //模版id wxMssVo.setTemplate_id(templateId); //模板内容 Map<String, TemplateData> data = new HashMap<>(); //服务项目 TemplateData name1 = new TemplateData(); name1.setValue("XX通知"); data.put("name1",name1); //时间 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); TemplateData date3 = new TemplateData(); date3.setValue(sf.format(new Date())); data.put("date3",date3); //服务说明 TemplateData thing5 = new TemplateData(); thing5.setValue("您已XX成功。"); data.put("thing5",thing5); wxMssVo.setData(data); //获取access_token String access_token = getAccess_token(weChatProperties.getAppId(), weChatProperties.getAppSecret()); String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send" + "?access_token=" + access_token; ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, wxMssVo, String.class); if (HttpStatus.OK != responseEntity.getStatusCode()){ log.error("小程序推送结果={}", responseEntity.getBody()); } JSONObject result = JSON.parseObject(responseEntity.getBody()); if(result.getInteger("errcode") != ResultCode.SUCCESS){ log.error("小程序推送结果={}", responseEntity.getBody()); }else{ log.info("小程序推送结果={}", responseEntity.getBody()); }
结束