SpringBoot 实现微信推送模板
导读
由于最近手头上需要做个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 "推送失败";
}
}
}
推送消息

SpringBoot 实现微信推送模板的更多相关文章
- 使用ESP8266nodeMCU 向微信推送模板数据
使用HTTPS协议向微信公众号推送消息,(使用ESP8266的低成本实现) 前几天被朋友问到这个东西的实现方式,花了一下午时间研究一下,特此记录.没有排版比较乱. 一丶前往微信公众平台注册微信微信公众 ...
- python 微信推送模板消息
#!/usr/bin/env python #-*- coding: utf-8 -*- import httplib import json import MySQLdb #从数据库中获取acces ...
- 微信公众号实现无限制推送模板消息!可向指定openID群发
微信认证的服务号才有推送模板消息接口所以本文需要在认证服务号的情况下学习 以上就是模板消息,只有文字和跳转链接,没有封面图.在服务号的后台添加功能插件-模板消息即可. 模板消息,都是在后台选择一个群发 ...
- 微信开发之获取openid及推送模板消息
有很多的朋友再问我怎么获取code,openid之类的问题,在这里我就给大家分享一下. 在做微信支付是需要获取openid的,推送模板消息也是需要openid包括其他一些功能分享等也都是需要的,ope ...
- C#微信接口之推送模板消息功能示例
本文实例讲述了C#微信接口之推送模板消息功能.分享给大家供大家参考,具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2 ...
- day93之微信推送
python之微信推送详解 用什么推送 -邮件 -微信推送 -短信推送微信推送 -公众号(不能主动给用户发消息) -认证的公众号:需要营业执照,需要交钱,可以发多篇文章 - ...
- 让微信推送Jenkins构建消息
Jenkins作为开发必备之神器,各家大小公司都在使用.Jenkins自身内置了基于邮件推送构建结果的功能.但是随着移动互联网的发展,邮件这玩意已经越来越少使用了,是否有一种办法能把jenkins构建 ...
- Docker系列——Grafana+Prometheus+Node-exporter微信推送(三)
在之前博文中,已经成功的实现了邮件推送.目前主流的办公终端,就是企业微信.钉钉.飞书.今天来分享下微信推送,我们具体来看. 企业微信 在配置企业微信推送时,需要有微信企业,具体如何注册.使用,另外百度 ...
- C# 推送模板
C#推送模板.安卓个推.消息推送 http://docs.getui.com/server/csharp/template/
随机推荐
- Redis事务使用方法
Redis事务 Redis事务是一组命令的集合,也是Redis的最小执行单位之一.一个事务的所有命令,要么都执行,要么都不执行.Redis能保证事务执行期间不会有其他命令插入. 相关命令 命令 格式 ...
- VC中句柄、指针、ID之间的转换
win32直接操作的是句柄HANDLE,每个句柄就对应windows窗口,而vc对HANDLE进行类封装,间接操作的都是HANDLE,现在句柄只是类的一个成员变量. 从句柄到指针 CWnd* pWnd ...
- 简单入门Rabbitmq
什么是RabbitMQ RabbitMQ是一个开源的AMQP实现,服务器端用Erlang语言编写.支持多种客户端,如:Python.Ruby..NET.Java.JMS.C.PHP.ActionScr ...
- JAVA每日一题20201109
一.标识符的规则? 1.严格区分大小写,不能使用关键字,保留字,不能重复 2.数字不能开头 二.标识符的命名规范 包名:XXXYYYZZZ 类名,接口名:XxYyZz 变量名,方法名:xxxYyyZz ...
- 美团笔试题_ACM最多队伍
题目: 选择队伍参加ACM比赛,可以选择的人由N+M组成,N代表N个擅长算法的人,M代表M个擅长编程的人,选择要求:每个队伍三个人,每个队伍中至少有一个擅长算法的人,至少有一个擅长编程的人.求可以组成 ...
- Round 4
最近再次经历动荡期 博客只在小白时期记录过 已经沉寂许久 之前的工作在黄区 加班超多 阻隔了一切与外网交流的可能 只能凭记忆补一点最近一年来积累到的知识 不管怎么样 不能放弃自己啊老铁!
- 变强——GitHub 热点速览 Vol.46
作者:HelloGitHub-小鱼干 网络不通怎么办?Ping 就是你的调试大招,而 gping 则是 Ping 的内功,终端可视化显示 Ping 结果,一眼看明数据接收情况.前端调试大招又是什么呢? ...
- 一:NOSQL
NOSQL =not only SQL 意即为不仅仅是SQL 传统的关系数据库在处理web2.0网站,特别是超大规模和高并发的社交网络服务类型的web2.0纯动态网站已经显得力不从心,出现了很多难以克 ...
- Python_爬虫_urllib解析库
简介:提取网页保存到txt文件中 + 解析txt文件内容,取出内容 from urllib import request import re.json url="http://www.163 ...
- redis部署安装【建议收藏】
一.redis安装教程 1.安装redis ~]# yum -y install gcc gcc-c++ make ~]# tar -xf redis-4.0.8.tar.gz ~]# cd redi ...