Springboot结合微信公众号(一) 发送模板消息提醒

1.  向微信公众号推算消息的代码很简单,如下所示.但必须先获取以下内容

(1)公众号的APPID,APPSECRET,

(2)消息模板templateId.

(3)接收消息的用户openid, 对于不同的公众号, 同一个用户的openid不一样.

 public static  final String APPID="";        //公众号APPID
 public static  final String APPSECRET="";    //公众号APPSECRET
 public static  final String templateId="";   //模板ID


public  void push(String first,String keyword1,String keyword2,String keyword3,String UserOpenId) {

        WxMpDefaultConfigImpl wxStorage = new WxMpDefaultConfigImpl();
        wxStorage.setAppId(APPID);
        wxStorage.setSecret(APPSECRET);
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
  
        //数据
        List<WxMpTemplateData> data = Arrays.asList(
                new WxMpTemplateData("first", first),
                new WxMpTemplateData("keyword1", keyword1),
                new WxMpTemplateData("keyword2", keyword2),
                new WxMpTemplateData("keyword3", keyword3)
        );

        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(UserOpenId)//要推送的用户openid
                .data(data) //数据
                .templateId(templateId)//模版id
                .build();

        try {
            String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            System.out.println("推送成功:" + msg);
        } catch (Exception e) {
            System.out.println("推送失败:" + e.getMessage());
            e.printStackTrace();
        }

    }

所需依赖 

<dependency>
   <groupId>com.github.binarywang</groupId>
   <artifactId>weixin-java-mp</artifactId>
   <version>3.8.0</version>
</dependency>

2.  公众号的APPID,APPSECRET,和消息模板templateId. 直接在公众号后台就能获得,无需写代码.

3.  接收消息的用户openid, 对于不同的公众号, 同一个用户的openid不一样. 所以得通过邦定获取.具体方法详见下文

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值