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

点个赞哦

背景

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

在做消息推送时也是踩了无数坑,因为在2021年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
@Service
public class SendMiniAppServiceImpl implements SendMiniAppService {

    @Autowired
    private WxMiniConfigVo wxMiniConfigVo;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public 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.weixin.qq.com/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;
    }

    @Override
    public String push(String openid, Map<String, String> content) {
        //获取用户token
        String token = String.valueOf(redisUtil.get("wxMiniToken"));
        if (token == "null") {
            token = getAccessToken();
        }
        String resultStatus = "0";//0:失败,1:成功
        try {
            //小程序统一消息推送
            String path = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=" + token;
            //封装参数
            JSONObject jsonData = new JSONObject();
            jsonData.put("touser", openid); // 小程序openid

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("appid", wxMiniConfigVo.getWxGzhId());// 公众号id
            jsonObject.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 stub
        JSONObject json = new JSONObject();
        json.put("value", value);
        json.put("color", "#173177");
        return json;
    }

  • 6
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值