微信公众号推送模板消息给用户
前置条件:
2.认证(300元)
5.编码(可以先看下接口文档)【以下为无跳转功能示例代码】
5.2 编码(可以先看下接口文档)【模板消息跳转到小程序】
public String test() {
String templateId = "NtMhq-lqMiaORC4L95J_aODMzf-OhPZpD1dAgW7qvWs";
Map<Object, WeChatTemplateMsg> sendMap = new HashMap<>();
sendMap.put("first", new WeChatTemplateMsg("您好,有一个设备离线通知~"));
sendMap.put("keyword1", new WeChatTemplateMsg("keyword1"));
sendMap.put("keyword2", new WeChatTemplateMsg("keyword2"));
sendMap.put("keyword3", new WeChatTemplateMsg("keyword3"));
sendMap.put("keyword4", new WeChatTemplateMsg("keyword4"));
sendMap.put("remark", new WeChatTemplateMsg("remark"));
MiniprogramDto miniprogramDto = new MiniprogramDto();
miniprogramDto.setAppid(wxMiniPromAppId);
//跳转到的小程序的界面路径(page.json中的path参数)可带参数,小程序端 onload(options) 接收参数
miniprogramDto.setPagepath("pages/xxxx/index?storeId=" + 1);
Map<String, Object> map = getNotifyMapWithMiniProm("oI9SZ6Jt-myBmVhbaGxOJuglUjBY", templateId, sendMap, miniprogramDto);
ResponseEntity<String> forEntity = restTemplate.postForEntity(NOTIFY_URL + getAccessToken(), map, String.class);
log.error("通知管理员设备状态完成。请求参数:{},响应参数:{}", map, forEntity);
return null;
}
private Map<String, Object> getNotifyMapWithMiniProm(String openId, String templateId, Map<Object, WeChatTemplateMsg> sendMap, MiniprogramDto dto) {
Map<String, Object> sendBody = new HashMap<>();
sendBody.put("touser", openId); // openId
sendBody.put("template_id", templateId); // 模板Id
sendBody.put("miniprogram", dto); // 小程序信息
sendBody.put("topcolor", "#FF0000"); // 顶色
sendBody.put("data", sendMap); // 模板参数
return sendBody;
}
@Data
public class MiniprogramDto {
private String appid;
private String pagepath;
}
6.结果
上一篇:
uniapp开发微信小程序-2.页面制作
下一篇:
微信协议,个人微信api接口
