当前位置:网站首页>【项目实训】微信公众号模板消息推送
【项目实训】微信公众号模板消息推送
2022-06-12 01:13:00 【par_ser】
首先,需要先获得一个模板
如果不是测试号,那么需要申请;如果是测试号,那么简单地在测试号管理页面新增测试模板即可。

引入依赖
为了简化开发,我们引入一个第三方的微信开发的工具包
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>3.3.0</version>
</dependency>代码
将参数放在application配置文件中
@Value("${wx.appid}")
private String appid;
@Value("${wx.secret}")
private String secret;
@Value("${wx.templateId}")
private String templateId; public void pushMessage(AlarmParamsDTO alarmParamsDTO) {
//1,配置
WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
wxStorage.setAppId(appid);
wxStorage.setSecret(secret);
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxStorage);
List<WxMpTemplateData> wxMpTemplateData = Arrays.asList(
new WxMpTemplateData("first",alarmParamsDTO.getFirst(),"#000000"),
new WxMpTemplateData("keyword1",alarmParamsDTO.getKeyword1(),"#0000FF"),
new WxMpTemplateData("keyword2",alarmParamsDTO.getKeyword2(),"#0000FF"),
new WxMpTemplateData("keyword3",alarmParamsDTO.getKeyword3(),"#0000FF"),
new WxMpTemplateData("keyword4",alarmParamsDTO.getKeyword4(),"#0000FF"),
new WxMpTemplateData("keyword5",alarmParamsDTO.getKeyword5(),"#0000FF"),
new WxMpTemplateData("remark",alarmParamsDTO.getRemark(),"#00FF00")
);
//2,推送消息
WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
.toUser(alarmParamsDTO.getOpenId())
.templateId(templateId)
.data(wxMpTemplateData)
.build();
try {
wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
} catch (Exception e) {
System.out.println("推送失败:" + e.getMessage());
}
}边栏推荐
- SQL exercise summary 3
- Lambda中间操作flatMap
- 我在某大厂做软件测试工程师的《一天完整工作流程》
- 只会基础的功能测试的我,被炒鱿鱼了····
- Low code platform design exploration, how to better empower developers
- DDD exaggeration, Eric Evans made a bad start
- Make good use of these 28 tools, and the development efficiency soars
- Lambda intermediate operation skip
- 手写MapReduce程序详细操作步骤
- New knowledge: monkey improved app crawler
猜你喜欢

'virtue and art' in the field of recurrent+transformer video recovery

Some suggestions on writing code to reproduce the paper!

Low code platform design exploration, how to better empower developers

100 deep learning cases | day 41: speech recognition - pytorch implementation

Make good use of these 28 tools, and the development efficiency soars

Matlab 基础应用02 wind 股票数据介绍和使用案例:

手写MapReduce程序详细操作步骤

Component introduction - large screen cloud minimalist user manual

Ms-hgat: information diffusion prediction based on memory enhanced sequence hypergraph attention network

System.CommandLine选项Option
随机推荐
Lambda termination operation foreach
Equipment encryption of industrial control security
Practice of Flink CDC + Hudi massive data entering the lake in SF
Blue Bridge Cup - 2012b Group real question 3 specific drinking capacity
In depth description of Weibull distribution (1) principle and formula
[answer] what does UML use to represent hexagonal architecture
Advanced data storage
Investment analysis and prospect forecast report of wearable biosensor industry for global and Chinese medical monitoring 2022 ~ 2028
flowable 工作流
出门带着小溪
In depth description of Weibull distribution (2) meaning of parameters and formulas
Lambda中间操作limit
Lambda中间操作sorted
The CSV used for JMeter performance test is bullshit
Vscode - the problem of saving a file and automatically formatting the single quotation mark 'into a double quotation mark'
MS-HGAT: 基于记忆增强序列超图注意力网络的信息扩散预测
Recurrent+Transformer 视频恢复领域的‘德艺双馨’
Lambda intermediate operation map
Given a project, how will you conduct performance testing?
手写MapReduce程序详细操作步骤