導讀
由於最近手頭上需要做個Message Gateway,涉及到:郵件(點我直達)、短信、公眾號等推送功能,網上學習下,整理下來以備以后使用。
添加依賴
在SpringBoot項目中添加依賴
<!--微信模版消息推送三方sdk--> <dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-mp</artifactId> <version>3.3.0</version> </dependency>
控制層代碼
package com.ybchen.springbootwechart.controller; 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.WxMpTemplateData; import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @ClassName:PushController * @Description:微信推送 * @Author:chenyb * @Date:2020/11/27 10:33 上午 * @Versiion:1.0 */ @RestController public class PushController { /* * 微信測試賬號推送 * */ @GetMapping("/push") public String push() { //1,配置 WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage(); wxStorage.setAppId("AppId"); wxStorage.setSecret("Secret"); WxMpService wxMpService = new WxMpServiceImpl(); wxMpService.setWxMpConfigStorage(wxStorage); //2,推送消息 WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder() .toUser("ojPPk54RcFkCgGVP3m66v1RM2mvA")//要推送的用戶openid .templateId("a7RPsASc7fw33zFo7zEfWKE0vrPnUo7VZ82fX3tTfMg")//模版id .url("https://www.cnblogs.com/chenyanbin/")//點擊模版消息要訪問的網址 .build(); //3,如果是正式版發送模版消息,這里需要配置你的信息 // templateMessage.addData(new WxMpTemplateData("name", "value", "#FF00FF")); // templateMessage.addData(new WxMpTemplateData(name2, value2, color2)); try { wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage); return "推送成功"; } catch (Exception e) { System.out.println("推送失敗:" + e.getMessage()); e.printStackTrace(); return "推送失敗"; } } }
去微信公眾平台注冊一個開發測試賬戶
個人開發,我們可以去微信公眾號平台注冊個測試賬戶點我直達,微信掃碼登錄,會給我們一個免費的:appID、appsecret,微信掃碼關注公眾號,會顯示關注測試公眾號的用戶列表。全局錯誤碼:點我直達
測試
關注測試公眾號,創建模板,並發送指定模板內容
替換模板內容
在微信公眾平台創建模板
語法:{{變量名.DATA}}
姓名:{{user_name.DATA}}
性別:{{sex.DATA}}
手機號:{{phone.DATA}}
郵箱:{{email.DATA}}
控制層修改

package com.ybchen.springbootwechart.controller; 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.WxMpTemplateData; import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Map; import java.util.Objects; /** * @ClassName:PushController * @Description:微信推送 * @Author:chenyb * @Date:2020/11/27 10:33 上午 * @Versiion:1.0 */ @RestController public class PushController { /* * 微信測試賬號推送 * */ @GetMapping("/push") public String push() { //1,配置 WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage(); wxStorage.setAppId("wx12db1518efd2302c"); wxStorage.setSecret("056f31d80a5a22cc0c418cc08f5657ad"); WxMpService wxMpService = new WxMpServiceImpl(); wxMpService.setWxMpConfigStorage(wxStorage); //2,推送消息 WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder() .toUser("ojPPk54RcFkCgGVP3m66v1RM2mvA")//要推送的用戶openid .templateId("O0t0lPP7xRqbNz0-OwPzliSplzGFrkr4-au-OIGhiOE")//模版id .url("https://www.cnblogs.com/chenyanbin/")//點擊模版消息要訪問的網址 .build(); //3,如果是正式版發送模版消息,這里需要配置你的信息,替換微信公眾號上創建的模板內容 templateMessage.addData(new WxMpTemplateData("user_name", "陳彥斌", "#CCCCFF")); templateMessage.addData(new WxMpTemplateData("sex", "男", "#FF00FF")); templateMessage.addData(new WxMpTemplateData("phone", "188888888888", "#CCFF99")); templateMessage.addData(new WxMpTemplateData("email", "543210188@qq.com", "#FF0033")); try { wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage); return "推送成功"; } catch (Exception e) { System.out.println("推送失敗:" + e.getMessage()); e.printStackTrace(); return "推送失敗"; } } }
推送消息