1. 程式人生 > 其它 >u3d 模版測試 失敗_5行程式碼實現微信模版訊息推送,springboot微信推送 java微信推送...

u3d 模版測試 失敗_5行程式碼實現微信模版訊息推送,springboot微信推送 java微信推送...

技術標籤:u3d 模版測試 失敗

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

先看效果圖:

8562f7d5d5dfd17249721a86e4c55cfe.png

核心程式碼只有下面幾行,即可輕鬆實現微信模版訊息推送

 //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檔案裡引入下面類庫
  com.github.binarywang weixin-java-mp 3.3.0
  • 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: 模版訊息推送模擬 */@RestControllerpublic 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