当前位置:网站首页>【项目实训】微信公众号模板消息推送
【项目实训】微信公众号模板消息推送
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());
}
}边栏推荐
- How to optimize the opening speed of website pages to improve the experience?
- 河南中创|从云到边,边缘计算如何赋能数据中心
- 市场监管总局、国家网信办:开展数据安全管理认证工作
- Kill, pkill, killall, next, what I brought to you in the last issue is how to end the process number and rush!
- Global and Chinese chromatographic silica gel resin industry research and investment direction forecast report 2022 Edition
- Uniapp gets the instance of the previous page
- The CSV used for JMeter performance test is bullshit
- Dr. wangxiting: understanding interpretable recommendation from the perspective of knowledge map and natural language generation
- Learn to crawl steadily 07 - detailed explanation of how to use XPath
- VsCode - 保存文件自动格式化将单引号 ‘ 变成双引号 “ 的问题
猜你喜欢

Redis advanced - correspondence between object and code base

Weekly CTF 第一周:神奇的磁带

Esp8266wifi development board collects temperature and humidity data and uploads them to the Internet of things platform
![[roe] (2) roe agreement](/img/5a/598da9b140216df046698766de41bb.png)
[roe] (2) roe agreement

Matlab 基础04 - 冒号Colon operator “:”的使用和复杂应用详析

How can functional tests be quickly advanced in one month? It is not a problem to clarify these two steps

Weibull Distribution韦布尔分布的深入详述(1)原理和公式

Nat. Comm. | supercomputing +ai: providing navigation for natural product biosynthesis route planning
![2022-06-11: note that in this document, graph is not the meaning of adjacency matrix, but a bipartite graph. In the adjacency matrix with length N, there are n points. Matrix[i][j] represents the dist](/img/ab/b3044472561ac366f2cbd878268132.png)
2022-06-11: note that in this document, graph is not the meaning of adjacency matrix, but a bipartite graph. In the adjacency matrix with length N, there are n points. Matrix[i][j] represents the dist

100 deep learning cases | day 41: speech recognition - pytorch implementation
随机推荐
In depth description of Weibull distribution (1) principle and formula
大厂测试员年薪30万到月薪8K,吐槽工资太低,反被网友群嘲?
Creating a flutter high performance rich text editor - rendering
河南中创|从云到边,边缘计算如何赋能数据中心
Data visualization big screen - big screen cloud minimalist user manual
'virtue and art' in the field of recurrent+transformer video recovery
Current situation investigation and demand forecast report of global and Chinese phenolic resin market, 2022 Edition
给你一个项目,你将如何开展性能测试工作?
Ms-hgat: information diffusion prediction based on memory enhanced sequence hypergraph attention network
Lambda创建流
Analysis report on operation trends and development strategies of global and Chinese plastic adhesive industry 2022-2028
Lambda终结操作forEach
How much is the child's critical illness insurance coverage appropriate? Which product is better now
Investment analysis and prospect forecast report of wearable biosensor industry for global and Chinese medical monitoring 2022 ~ 2028
How to optimize plantuml flow diagram (sequence diagram)
2022-06-11:注意本文件中,graph不是邻接矩阵的含义,而是一个二部图。 在长度为N的邻接矩阵matrix中,所有的点有N个,matrix[i][j]表示点i到点j的距离或者权重, 而在二部
Defect detection, introduction to Halcon case.
Virtual human appears on the stage of the Winter Olympic Games, connecting elements of the meta universe
How to guarantee industrial control safety: system reinforcement
VM tools fail in VMware? Install VM tools using Ali image source