失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 小程序关联公众号推送消息(20行代码搞定。公众平台最新版推送接口文档-傻瓜式复制粘

小程序关联公众号推送消息(20行代码搞定。公众平台最新版推送接口文档-傻瓜式复制粘

时间:2023-12-29 21:48:27

相关推荐

小程序关联公众号推送消息(20行代码搞定。公众平台最新版推送接口文档-傻瓜式复制粘

点个赞哦

背景:

最近开发的一个小程序项目需要通过服务号来推送通知。但是在最开始开发小程序的时候使用了一次性消息推送,不是很友好。

在做消息推送时也是踩了无数坑,因为在2月前其他人的文章所使用的案例中调用的小程序模板消息接口都没有用,也没有实质性的帮助,所以我在这篇文章中给出自己摸索出针对统一消息接口做推送的解决方案。

1.首先将小程序绑定到指定的公众号下

2.申请公众号模板

3.yml中增加小程序和公众号id及消息模板配置

4.增加配置类

@Data@Component@ConfigurationProperties()public class WxMiniConfigVo {/**小程序appid*/@Value("${wxmini.id}")private String WxMiniAPPID;/**小程序秘钥*/@Value("${wxmini.secret}")private String WxMiniSECRET;/**小程序数据加密方式*/@Value("${wxmini.grant_type}")private String WxMinigrantType;/**公众号id*/@Value("${wxmini.gzh_id}")private String WxGzhId;/**公众号模板*/@Value("${wxmini.gzh_template}")private String WxGzhTemplate;}

5.编写接口

@Slf4j@Servicepublic class SendMiniAppServiceImpl implements SendMiniAppService {@Autowiredprivate WxMiniConfigVo wxMiniConfigVo;@Autowiredprivate RedisUtil redisUtil;@Overridepublic String getAccessToken() {RestTemplate restTemplate = new RestTemplate();Map<String, String> params = new HashMap<>();params.put("APPID", wxMiniConfigVo.getWxMiniAPPID());params.put("APPSECRET", wxMiniConfigVo.getWxMiniSECRET());params.put("grant_type", "client_credential");String tokenUrl="https://api./cgi-bin/token?appid={APPID}&secret={APPSECRET}&grant_type={grant_type}";ResponseEntity<String> responseEntity = restTemplate.getForEntity(tokenUrl, String.class, params);String body = responseEntity.getBody();JSONObject object = JSON.parseObject(body);String Access_Token = object.getString("access_token");redisUtil.set("wxMiniToken", Access_Token);redisUtil.expire("wxMiniToken", Long.parseLong(object.getString("expires_in")));return Access_Token;}@Overridepublic String push(String openid, Map<String, String> content) {//获取用户tokenString token = String.valueOf(redisUtil.get("wxMiniToken"));if (token == "null") {token = getAccessToken();}String resultStatus = "0";//0:失败,1:成功try {//小程序统一消息推送String path = "https://api./cgi-bin/message/wxopen/template/uniform_send?access_token=" + token;//封装参数JSONObject jsonData = new JSONObject();jsonData.put("touser", openid); // 小程序openidJSONObject jsonObject = new JSONObject();jsonObject.put("appid", wxMiniConfigVo.getWxGzhId());// 公众号idjsonObject.put("template_id", wxMiniConfigVo.getWxGzhTemplate());// 公众号消息模板id//公众号消息数据封装JSONObject data = new JSONObject();data.put("first", getValue("请注意!有新告警产生"));//标题data.put("keyword1", getValue(content.get("deviceId")));//设备编号data.put("keyword2", getValue(content.get("alarmName")));//设备名称data.put("keyword3", getValue(content.get("alarmAddress")));//设备地址data.put("keyword4", getValue(content.get("content")));//告警描述data.put("keyword5", getValue(content.get("alarmTime")));//告警时间data.put("remark", getValue("请及时排查并处理!"));jsonObject.put("data", data);jsonData.put("mp_template_msg", jsonObject);HttpRequest.sendPost(path, jsonData.toJSONString());resultStatus="1";} catch (Exception e) {log.error("微信公众号发送消息失败!",e.getMessage());resultStatus="0";}return resultStatus;}private JSONObject getValue(String value) {// TODO Auto-generated method stubJSONObject json = new JSONObject();json.put("value", value);json.put("color", "#173177");return json;}

小程序关联公众号推送消息(20行代码搞定。公众平台最新版推送接口文档-傻瓜式复制粘贴)

如果觉得《小程序关联公众号推送消息(20行代码搞定。公众平台最新版推送接口文档-傻瓜式复制粘》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。