今天來帶大家學習下微信模版消息推送。
先看效果圖:
核心代碼只有下面幾行,即可輕鬆實現微信模版消息推送
//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
-
2,微信掃碼關注 測試號二維碼,微信給我們返回我們的openid,這個openid在推送時特別重要。因爲你推送肯定要知道推送給 誰啊,就比如你打電話,肯定要知道用戶的電話號碼吧。這個openid就是我們要推送給那個用戶的唯一標示。
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();
}
}
單元測試其實很簡單,我們只需要點擊箭頭所指的綠色按鈕,即可運行單元測試,運行通過後就可以看到發送消息成功了。
log裏可以看出我們是10:46發起推送的,看下圖我們微信接受到的推送消息也是10:46
運行springboot,通過get請求來觸發推送
這個就更簡單了,我們啓動springboot項目,然後調用get請求:
可以看到我們也推送成功了。
到這裏我們就輕鬆通過簡單幾行代碼實現了微信模版消息推送的功能了。
我們在企業生產環境時,實現這個功能,步驟和這裏是一樣的。代碼也和這裏差不多,只不過多了一個獲取用戶openid的步驟,這個步驟微信要求比較嚴格,必須要有備案的網址作爲回調,今天就不給大家深入講解了,後期我會專門寫一篇獲取微信用戶openid的文章出來。
如果你有微信或者java開發方面的問題,可以加我微信交流學習:2501902696。也可以加我微信獲取完整源碼。