java 使用微信公众号模板推送消息

 <!--微信模版消息推送三方sdk-->
       <dependency>
           <groupId>com.github.binarywang</groupId>
           <artifactId>weixin-java-mp</artifactId>
           <version>3.3.0</version>
       </dependency>

1.配置文件

## 微信公众号
wechat:
  appId: wxb49343432a366034c68543495376
  secret: 3243243254354
  IN:
    templateId: UBSycw0USbnMni32NOH042x43ZCPUHW3UcqCsYJNYo
    url: http://123.net/
  OUT:
    templateId: UBSycw0USbnMn123iNOH5350xZCPUHW3UcqCsYJINYo
    url: http://123.net/

2.推送接口

	@Async
    @Override
    public void push(WechatDto wechatDto) {
        //1,配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(env.getProperty("wechat.appId"));
        wxStorage.setSecret(env.getProperty("wechat.secret"));
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);

        if (null == wechatDto) {
            return;
        }

        List<String> openIdList = getOpenIdList(wechatDto.getCustomerId());

        for (String openId : openIdList) {

            String templateId = env.getProperty("wechat." + wechatDto.getWay() + ".templateId");
            String url = env.getProperty("wechat." + wechatDto.getWay() + ".url");
            //2,推送消息
            WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                    .toUser(openId)//要推送的用户openid
                    .templateId(templateId)
                    .url(url)
                    .build();


            //3,如果是正式版发送模版消息,这里需要配置你的信息
            templateMessage.addData(new WxMpTemplateData("first", wechatDto.getFirst(), "#000000"));
            templateMessage.addData(new WxMpTemplateData("keyword1", wechatDto.getKeyword1(), "#000000"));
            templateMessage.addData(new WxMpTemplateData("keyword2", wechatDto.getKeyword2(), "#22295F"));
            templateMessage.addData(new WxMpTemplateData("keyword3", wechatDto.getKeyword3(), "#22295F"));
            templateMessage.addData(new WxMpTemplateData("keyword4", wechatDto.getKeyword4(), "#22295F"));
            templateMessage.addData(new WxMpTemplateData("remark", wechatDto.getRemark(), "#22295F"));
            try {
                wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            } catch (Exception e) {
                System.out.println("推送失败:" + e.getMessage());
                e.printStackTrace();
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值