前置条件:
1.公众号为服务号,而非订阅号
2.认证(300元)
3.进入公众号申请模板推送功能
4.添加模板(注意:推送的消息只能使用微信提供的模板,不可自定义,但也是比较全的)
4.2 获取accessToken时,需要将开发环境的电脑ip添加到微信后台的ip白名单(线上环境亦是如此)
5.编码(可以先看下接口文档)【以下为无跳转功能示例代码】
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"));Map<String, Object> map = getNotifyMap("oI9SZ6Jt-myBmVhbaGxOJuglUjBY", templateId, sendMap);ResponseEntity<String> forEntity = restTemplate.postForEntity(NOTIFY_URL + getAccessToken(), map, String.class);log.error("通知管理员设备状态完成。请求参数:{},响应参数:{}", map, forEntity);return null;}//通用通知参数封装private Map<String, Object> getNotifyMap(String openId, String templateId, Map<Object, WeChatTemplateMsg> sendMap) {Map<String, Object> sendBody = new HashMap<>();sendBody.put("touser", openId); // openIdsendBody.put("template_id", templateId);// 模板IdsendBody.put("topcolor", "#FF0000");// 顶色sendBody.put("data", sendMap); // 模板参数return sendBody;}//获取token(此处加了个redis,可选择不加,每次调接口获取也可)public String getAccessToken() {String accessToken;//0.从redis中检索,若没有则调用接口获取boolean hasKey = redisOprUtil.hasKey(WX_ACCESS_TOKEN_KEY);if (hasKey) {accessToken = redisOprUtil.get(WX_ACCESS_TOKEN_KEY).toString();log.error("redis中获取access_token......");} else {//1.调用接口获取String url = "https://api./cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret;String resp = restTemplate.getForObject(url, String.class);log.error("获取token的响应:{}", resp);JSONObject jsonObject = JSONObject.parseObject(resp);accessToken = jsonObject.get("access_token").toString();int expiresIn = Integer.parseInt(jsonObject.get("expires_in").toString());//并将结果保存到redis中boolean saveAccessToken = redisOprUtil.set(WX_ACCESS_TOKEN_KEY, accessToken, expiresIn);log.error("微信接口获取access_token......且已保存到redis中:{}", saveAccessToken);}return accessToken;}
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); // openIdsendBody.put("template_id", templateId);// 模板IdsendBody.put("miniprogram", dto);// 小程序信息sendBody.put("topcolor", "#FF0000");// 顶色sendBody.put("data", sendMap); // 模板参数return sendBody;}@Datapublic class MiniprogramDto {private String appid;private String pagepath;}
6.结果