WeChat official account push template message to users

Preconditions:

1. The official account is a service account, not a subscription account

2. Certification (300 yuan)

3. Enter the official account to apply for the template push function

4. Add a template (note: the pushed message can only use the template provided by WeChat, which cannot be customized, but it is relatively complete)

4.2 When obtaining accessToken, you need to add the computer ip in the development environment to the ip whitelist in the background of WeChat (the same is true for the online environment)

 

5. Coding (you can read the interface document first) [The following is a sample code without jump function]

 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); // openId
        sendBody.put("template_id", templateId);     // 模板Id
        sendBody.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.weixin.qq.com/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 Encoding (you can read the interface document first) [Template message jumps to applet]

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. Results

 

 

 

Guess you like

Origin blog.csdn.net/weiqiang915/article/details/126538106