spring boot 实现微信模板消息推送

    /*
     * 免费订单通知
     * */
    @Override
    public void push(@RequestParam("openid") String openid,@RequestParam("userid") String userid,@RequestParam("Username") String Username) {
        //1,配置

        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(env.getProperty("wxlog.appid"));
        wxStorage.setSecret(env.getProperty("wxlog.appsecret"));
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        String createtime= df.format(new Date());
        //2,推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openid)//要推送的用户openid
                .templateId("zwSiks9NaP8_lVXoSGuIwlj0Ic*****")//模版id
                .url("http://*********")//点击模版消息要访问的网址
                .build();
        //3,如果是正式版发送模版消息,这里需要配置你的信息

        templateMessage.addData(new WxMpTemplateData("first", "您有一个新的订单请你及时处理", "#FF00FF"));
        templateMessage.addData(new WxMpTemplateData("keyword1", Username, "#9A9A9A"));
        templateMessage.addData(new WxMpTemplateData("keyword2", "无", "#9A9A9A"));
        templateMessage.addData(new WxMpTemplateData("keyword3", "免费测斑", "#9A9A9A"));
        templateMessage.addData(new WxMpTemplateData("keyword4", createtime, "#9A9A9A"));
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (Exception e) {
            System.out.println("推送失败:" + e.getMessage());
            e.printStackTrace();
        }

    }

        
        
            com.github.binarywang
            weixin-java-mp
            3.3.0
        

 

你可能感兴趣的:(spring,boot,微信,微信模板消息推送)