导读

  由于最近手头上需要做个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 实现微信推送模板的更多相关文章

  1. 使用ESP8266nodeMCU 向微信推送模板数据

    使用HTTPS协议向微信公众号推送消息,(使用ESP8266的低成本实现) 前几天被朋友问到这个东西的实现方式,花了一下午时间研究一下,特此记录.没有排版比较乱. 一丶前往微信公众平台注册微信微信公众 ...

  2. python 微信推送模板消息

    #!/usr/bin/env python #-*- coding: utf-8 -*- import httplib import json import MySQLdb #从数据库中获取acces ...

  3. 微信公众号实现无限制推送模板消息!可向指定openID群发

    微信认证的服务号才有推送模板消息接口所以本文需要在认证服务号的情况下学习 以上就是模板消息,只有文字和跳转链接,没有封面图.在服务号的后台添加功能插件-模板消息即可. 模板消息,都是在后台选择一个群发 ...

  4. 微信开发之获取openid及推送模板消息

    有很多的朋友再问我怎么获取code,openid之类的问题,在这里我就给大家分享一下. 在做微信支付是需要获取openid的,推送模板消息也是需要openid包括其他一些功能分享等也都是需要的,ope ...

  5. 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 ...

  6. day93之微信推送

    python之微信推送详解 用什么推送   -邮件   -微信推送   -短信推送微信推送   -公众号(不能主动给用户发消息)    -认证的公众号:需要营业执照,需要交钱,可以发多篇文章    - ...

  7. 让微信推送Jenkins构建消息

    Jenkins作为开发必备之神器,各家大小公司都在使用.Jenkins自身内置了基于邮件推送构建结果的功能.但是随着移动互联网的发展,邮件这玩意已经越来越少使用了,是否有一种办法能把jenkins构建 ...

  8. Docker系列——Grafana+Prometheus+Node-exporter微信推送(三)

    在之前博文中,已经成功的实现了邮件推送.目前主流的办公终端,就是企业微信.钉钉.飞书.今天来分享下微信推送,我们具体来看. 企业微信 在配置企业微信推送时,需要有微信企业,具体如何注册.使用,另外百度 ...

  9. C# 推送模板

    C#推送模板.安卓个推.消息推送 http://docs.getui.com/server/csharp/template/

随机推荐

  1. HTTP协议(2)

    HTTP协议主要是有HTTP请求报文和HTTP响应报文组成的. HTTP请求报文主要分为四个部分: 第一部分:请求行.独占一行,由请求方法.请求url以及协议/版本组成: 第二部分:请求头,第二行到第 ...

  2. 调度《Taint(污点) 和 Toleration(容忍)》

    节点亲和性(详见这里),是 pod 的一种属性(偏好或硬性要求),它使 pod 被吸引到一类特定的节点.Taint 则相反,它使 节点 能够 排斥 一类特定的 pod. Taint 和 tolerat ...

  3. [Luogu P3959] 宝藏 (状压DP+枚举子集)

    题面 传送门:https://www.luogu.org/problemnew/show/P3959 Solution 这道题的是一道很巧妙的状压DP题. 首先,看到数据范围,应该状压DP没错了. 根 ...

  4. Elasticsearch 注册windows服务后,服务启动失败,意外终止

    直接双击elasticsearch.bat可以成功启动,注册成服务后就启动失败 从网上查找问题,发现是jdk版本的问题,用ES自带的jdk就可以启动成功. 默认ES会先找JAVA_HOME环境变量,如 ...

  5. BPMN开源工作流编辑器bpmn-js落地实践中文文档

    BPMN是一套标准的业务流程建模符号规范,bpmn-js是基于此规范实现的一套渲染工具包和web建模器,可以实现拖拽生成工作流程图,效果大概如下 最近刚好用到,研究之后写了系列文章,分享给有需要的小伙 ...

  6. Gromacs文件-Chapter1

    Gromacs的文件非常的多,这是官方文档地址:http://manual.gromacs.org/online/files.html. 本文章部分内容来自以下网址https://zhuanlan.z ...

  7. Socket listen 简要分析

    #include <sys/types.h> /* See NOTES */#include <sys/socket.h>int listen(int sockfd, int ...

  8. 三、分布式编程总结------linux多线程服务端编程

  9. Spring源码之@Configuration原理

    总结 @Configuration注解的Bean,在BeanDefinition加载注册到IOC容器之后,进行postProcessBeanFactory处理时会进行CGLIB动态代理 将@Prope ...

  10. 机器学习——dbscan密度聚类

    完整版可关注公众号:大数据技术宅获取 DBSCAN(Density-Based Spatial Clustering of Applications with Noise,基于密度的有噪应用中的空间聚 ...