当前位置:   article > 正文

java中微信订阅消息发送通知_java微信公众号订阅消息通知

java微信公众号订阅消息通知

一、开发中实际场景,例如用户余额不足时像用户发送余额不足通知

1.在微信公众平台申请订阅消息功能

2.编写测试类发送通知

1.获取token

2.组装报文

3.调用微信api

4.获取返回状态码成功,则为ok

  1. public void sendChargeMessage() {
  2. Token token = Token.getInstance();
  3. String accessToken = token.getAccessToken();
  4. System.out.println("我拿到token了++++++++" + accessToken);
  5. Map<String, WeChatTemplateData> data = new HashMap<>();
  6. WeChatTemplateData templateOrderIdData = new WeChatTemplateData();
  7. templateOrderIdData.setValue("");
  8. templateOrderIdData.setColor("#173177");
  9. WeChatTemplateData templateNameData = new WeChatTemplateData();
  10. templateNameData.setValue("");
  11. templateNameData.setColor("#173177");
  12. WeChatTemplateData templateTing5Data = new WeChatTemplateData();
  13. templateTing5Data.setValue("");
  14. templateTing5Data.setColor("#173177");
  15. WeChatTemplateData templateAmountData = new WeChatTemplateData();
  16. templateAmountData.setValue("");
  17. templateAmountData.setColor("#173177");
  18. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年M月d日 HH:mm");
  19. LocalDateTime currentDateTime = LocalDateTime.now();
  20. String formattedDateTime = currentDateTime.format(formatter);
  21. WeChatTemplateData templateTimeData = new WeChatTemplateData();
  22. templateTimeData.setValue(formattedDateTime);
  23. templateTimeData.setColor("#173177");
  24. data.put("character_string4", templateOrderIdData);
  25. data.put("thing1", templateNameData);
  26. data.put("thing3", templateTing5Data);
  27. data.put("amount2", templateAmountData);
  28. data.put("time5", templateTimeData);
  29. log.info("消息发送数据:{}", data.toString());
  30. String result = this.chargeMessage(accessToken, open_id, data);
  31. log.info("消息发送结果:", result);
  32. }
  33. /**
  34. * 发送余额不足消息
  35. *
  36. * @param accessToken
  37. * @param touser
  38. * @param map
  39. * @return
  40. */
  41. public String chargeMessage(String accessToken, String touser, Map<String, WeChatTemplateData> map) {
  42. WechatTemplate message = new WechatTemplate();
  43. //拼接数据
  44. message.setPage(messPath);
  45. message.setData(map);
  46. message.setTouser(touser);
  47. message.setTemplate_id(chargeTemId);
  48. message.setAccess_token(accessToken);
  49. message.setMiniprogram_state(type);//跳转小程序类型 “体验版”
  50. String json = com.alibaba.fastjson.JSONObject.toJSONString(message);
  51. System.out.println("要发送的数据" + json);
  52. String ret = URLUtils.sendPost("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken, json);
  53. return ret;
  54. }

3.封装的实体类WechatTemplateData

  1. public class WeChatTemplateData {
  2. private String value;
  3. private String color;
  4. public String getValue() {
  5. return value;
  6. }
  7. public void setValue(String value) {
  8. this.value = value;
  9. }
  10. public String getColor() {
  11. return color;
  12. }
  13. public void setColor(String color) {
  14. this.color = color;
  15. }
  16. }

点赞加关注,持续更新中!!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/674803
推荐阅读
相关标签
  

闽ICP备14008679号