關於SpringBoot開發微信模板推送


在這里演示一下微信的模板消息推送:

  這里使用微信測試號 來演示:

    先看下效果吧:

       

1.首先需要申請一個  微信測試號  

    https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421137522

    

 點擊進入申請之后會讓你掃碼登入!使用微信掃碼即可!

申請完成之后會顯示兩個測試號的信息 !appID ,appsecret 這兩個很重要!

 

2.關注此公眾號:

  

3.創建模板發送消息模板

  

由此獲得模板id

 

{{first.DATA}} 商家名稱:{{keyword1.DATA}} 商家電話:{{keyword2.DATA}} 訂單號:{{keyword3.DATA}} 狀態:{{keyword4.DATA}} 總價:{{keyword5.DATA}} {{remark.DATA}} 

 

4.回顧下我們都做了什么?

  1.首先是微信給我們的 【appIDappsecret

  2.我們需要微信掃碼關注測試的公眾號 獲得 【微信號】也就是用戶的openid

  3.創建模板 獲得模板id

===開始代碼的操作===

  首先需要一個jar包 也就是操作微信相關的jar包

    

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

 

  

 //推送
    @Override
    public void orderStatus(OrderDTO orderDTO) {
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId("wxc05384c53b8465d1");  //appID
        wxStorage.setSecret("e6b01b0374a48085e0e4ab521d68f7cf");//appsecretID
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);

            //數據
        List<WxMpTemplateData> data = Arrays.asList(
                new WxMpTemplateData("first", "親,請記得收貨。"),
                new WxMpTemplateData("keyword1", "微信點餐"),
                new WxMpTemplateData("keyword2", "18868812345"),
                new WxMpTemplateData("keyword3", orderDTO.getOrderId()),
                new WxMpTemplateData("keyword4", orderDTO.getOrderStatusEnum().getMessage()),
                new WxMpTemplateData("keyword5", "¥" + orderDTO.getOrderAmount()),
                new WxMpTemplateData("remark", "歡迎再次光臨!")
        );



        //2,推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser("otjjq1X7LiCG6gabzu2nk4Wzom0s")//要推送的用戶openid
                .data(data) //數據
                .templateId("OSIT_3CoEjwdgbGvHeUb5aO8wBu62-iH8PO2ZYopRkY")//模版id
              /*  .url("https://www.baidu.com/")//點擊模版消息要訪問的網址*/
                .build();
        //發起推送
         try {
             String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
             System.out.println("推送成功:" + msg);
         } catch (Exception e) {
             System.out.println("推送失敗:" + e.getMessage());
             e.printStackTrace();
         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM