• 當前位置:首頁 > IT技術 > 微信平臺 > 正文

    5行代碼實現微信模版消息推送,springboot實現微信推送,java微信推送
    2021-08-07 18:49:27

    ?

    今天來帶大家學習下微信模版消息推送。

    先看效果圖:
    5行代碼實現微信模版消息推送,springboot實現微信推送,java微信推送_java微信推送

    核心代碼只有下面幾行,即可輕松實現微信模版消息推送

            //1,配置
            WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
            wxStorage.setAppId("wx77bb69292323a000");
            wxStorage.setSecret("29bd368145806115ad6820133e62806e");
            WxMpService wxMpService = new WxMpServiceImpl();
            wxMpService.setWxMpConfigStorage(wxStorage);
            //2,推送消息
            WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                    .toUser("o5kho6DgC7SDry8zCmXuvHJGvrgI")//要推送的用戶openid
                    .templateId("Tpln-Eue2obJ0B-8JNkgkiRJaDMPgVeIgGxna982xrg")//模版id
                    .url("https://30paotui.com/")//點擊模版消息要訪問的網址
                    .build();
            try {
                wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            } catch (Exception e) {
                System.out.println("推送失?。? + e.getMessage());
            }
    

    所用知識點

    • 1, springboot實現java后臺
    • 2,微信測試賬號的申請
    • 3,微信模版推送的配置
      接下來就帶領大家來一步步實現微信模版消息推送。

    一,springboot創建java后臺

    至于springboot怎么創建java后臺,我這里就不再嘮叨了,大家百度一下,一大堆的文章。這里只需要重點講解下以下幾點。

    • 1,在pom.xml文件里引入下面類庫
            <!--微信模版消息推送三方sdk-->
            <dependency>
                <groupId>com.github.binarywang</groupId>
                <artifactId>weixin-java-mp</artifactId>
                <version>3.3.0</version>
            </dependency>
    
    • 2,寫一個推送的controller
    package com.qiushi.wxpush;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
    import me.chanjar.weixin.mp.api.WxMpService;
    import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
    import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
    
    /**
     * Created by qcl on 2019-03-28
     * 微信:2501902696
     * desc: 模版消息推送模擬
     */
    @RestController
    public class PushController {
    
    
        /*
         * 微信測試賬號推送
         * */
        @GetMapping("/push")
        public void push() {
            //1,配置
            WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
            wxStorage.setAppId("wx77bb69292323a000");
            wxStorage.setSecret("29bd368145806115ad6820133e62806e");
            WxMpService wxMpService = new WxMpServiceImpl();
            wxMpService.setWxMpConfigStorage(wxStorage);
    
            //2,推送消息
            WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                    .toUser("o5kho6DgC7SDry8zCmXuvHJGvrgI")//要推送的用戶openid
                    .templateId("Tpln-Eue2obJ0B-8JNkgkiRJaDMPgVeIgGxna982xrg")//模版id
                    .url("https://30paotui.com/")//點擊模版消息要訪問的網址
                    .build();
            //3,如果是正式版發送模版消息,這里需要配置你的信息
            //        templateMessage.addData(new WxMpTemplateData("name", "value", "#FF00FF"));
            //                templateMessage.addData(new WxMpTemplateData(name2, value2, color2));
            try {
                wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            } catch (Exception e) {
                System.out.println("推送失?。? + e.getMessage());
                e.printStackTrace();
            }
    
        }
    
    
    }
    

    二,接下來就來重點講講我們如何注冊微信測試賬號,并實現推送功能。

    正常我們企業開發,實現微信模版消息推送,必須要有微信公眾號,備案的網址,并且最麻煩的一點是要獲取到用戶的openid,作為個人,這些條件基本上都不具備。所以今天就來帶大家注冊微信開發測試賬號,來輕松實現微信模版消息推送。

    • 1,微信掃碼登錄下面網址
      https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
      掃碼登錄成功后,就會給我們生成微信公號的appid和appsecret
      5行代碼實現微信模版消息推送,springboot實現微信推送,java微信推送_java微信推送_02

    • 2,微信掃碼關注 測試號二維碼,微信給我們返回我們的openid,這個openid在推送時特別重要。因為你推送肯定要知道推送給 誰啊,就比如你打電話,肯定要知道用戶的電話號碼吧。這個openid就是我們要推送給那個用戶的唯一標示。
      5行代碼實現微信模版消息推送,springboot實現微信推送,java微信推送_java微信推送_03

    • 3,拿到這些以后,我們就可以去實現微信推送了。推送的代碼就只有下面這么點。

             //1,配置
            WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
            wxStorage.setAppId("wx77bb69292323a000");//appid
            wxStorage.setSecret("29bd368145806115ad6820133e62806e");//appsecret
            WxMpService wxMpService = new WxMpServiceImpl();
            wxMpService.setWxMpConfigStorage(wxStorage);
    
            //2,推送消息
            WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                    .toUser("o5kho6DgC7SDry8zCmXuvHJGvrgI")//要推送的用戶openid
                    .templateId("Tpln-Eue2obJ0B-8JNkgkiRJaDMPgVeIgGxna982xrg")//模版id
                    .url("https://30paotui.com/")//點擊模版消息要訪問的網址
                    .build();
            //3,如果是正式版發送模版消息,這里需要配置你的信息
            //        templateMessage.addData(new WxMpTemplateData("name", "value", "#FF00FF"));
            //                templateMessage.addData(new WxMpTemplateData(name2, value2, color2));
    
            //發起推送
            try {
                String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
                System.out.println("推送成功:" + msg);
            } catch (Exception e) {
                System.out.println("推送失?。? + e.getMessage());
                e.printStackTrace();
            }
    
    

    三,推送測試

    代碼都完成后,我們就可以來測試推送了。測試我們這個分兩種

    • 1,java的單元測試
    • 2,運行springboot,通過get請求來觸發推送

    單元測試

    package com.qiushi.wxpush;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import static org.junit.Assert.*;
    
    /**
     * Created by qcl on 2019-03-28
     * 微信:2501902696
     * desc:測試用例
     */
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class PushControllerTest {
    
        @Autowired
        PushController pushController;
    
        @Test
        public void push() {
            pushController.push();
        }
    }
    

    單元測試其實很簡單,我們只需要點擊箭頭所指的綠色按鈕,即可運行單元測試,運行通過后就可以看到發送消息成功了。
    5行代碼實現微信模版消息推送,springboot實現微信推送,java微信推送_微信模版推送_04
    log里可以看出我們是10:46發起推送的,看下圖我們微信接受到的推送消息也是10:46
    5行代碼實現微信模版消息推送,springboot實現微信推送,java微信推送_小程序推送_05

    運行springboot,通過get請求來觸發推送

    這個就更簡單了,我們啟動springboot項目,然后調用get請求:
    5行代碼實現微信模版消息推送,springboot實現微信推送,java微信推送_微信消息推送_06
    5行代碼實現微信模版消息推送,springboot實現微信推送,java微信推送_微信模版推送_07
    可以看到我們也推送成功了。

    到這里我們就輕松通過簡單幾行代碼實現了微信模版消息推送的功能了。

    我們在企業生產環境時,實現這個功能,步驟和這里是一樣的。代碼也和這里差不多,只不過多了一個獲取用戶openid的步驟,這個步驟微信要求比較嚴格,必須要有備案的網址作為回調,今天就不給大家深入講解了,后期我會專門寫一篇獲取微信用戶openid的文章出來。

    如果你有微信或者java開發方面的問題,可以加我微信交流學習:2501902696。也可以加我微信獲取完整

    本文摘自 :https://blog.51cto.com/u

    開通會員,享受整站包年服務
    国产呦精品一区二区三区网站|久久www免费人咸|精品无码人妻一区二区|久99久热只有精品国产15|中文字幕亚洲无线码