公眾號消息推送


今天去編寫測試公眾號的消息推送

消息推送需要到的jar包:weixin-java-common-3.1.0.jar和weixin-java-mp-3.2.0.jar

消息推送需要到的值:openid、appid、appsecret、AccessToken(https://mp.weixin.qq.com/debug/cgi-bin/apiinfo?t=index)、模板ID

 

 

 public static void push() {
        //1,配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId("wx7f1ceeace0d20918");
        wxStorage.setSecret("92f6aea5a9d4e3a93023ce1f2cb83e84");
        wxStorage.setAccessToken("32__PGUZdML3AtZoHzgFPUd1Y-QwG69yeFWXwM2fB5wBlRWWM8WfgK7D56MEXkxLZ3_HLj57-dGUSmc1sIYm-Mi2TeGc51Gq9j10KmHYmv8DU9dVHzymxkt9s5FQZOg0V5oJsdA8x90F9H7l_YAUZOjAJAYDS");
        
     WxMpService wxMpService
= new WxMpServiceImpl(); wxMpService.setWxMpConfigStorage(wxStorage);//配置加載到WxMpServiceImpl中 WxMpTemplateMessage templateMessage=WxMpTemplateMessage.builder()
     .toUser("oAGE3s-E3zvpad_p9F45vpFb4v_U")//openid
     .templateId("oi-mDbkiTZ7fH4MnhFADYQ6hhhb-sdBVndAzieNWpsE")//模板id
     .url("http://baidu.com").build();//跳轉地址
List
<WxMpTemplateData> templateData; templateMessage.addData(new WxMpTemplateData("first", "您好", "#FF00FF"))//模板數據添加 .addData(new WxMpTemplateData("keyword1", "這是個測試", "#A9A9A9")) .addData(new WxMpTemplateData("keyword2", "這又是個測試", "#FF00FF")) .addData(new WxMpTemplateData("keyword3", "這是個測試", "#A9A9A9")) .addData(new WxMpTemplateData("keyword4", "這又是個測試", "#FF00FF")) .addData(new WxMpTemplateData("remark", "這還是個測試", "#000000")); try { // templateMessage.setToUser("oAGE3s-E3zvpad_p9F45vpFb4v_U");//要推送的用戶openid wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage); } catch (Exception e) { System.out.println("推送失敗:" + e.getMessage()); e.printStackTrace(); } }

 

accessToken需要到https://mp.weixin.qq.com/debug/cgi-bin/apiinfo?t=index去配置獲取


免責聲明!

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



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