导读

  由于最近手头上需要做个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. Vulnhub DC3

    靶机简介 C-3是另一个专门建造的易受攻击的实验室,目的是获得渗透测试领域的经验.与以前的DC版本一样,这个版本是为初学者设计的,尽管这次只有一个标志,一个入口点,根本没有线索.Linux技能和熟悉L ...

  2. Spring 5的最后一个特性版本5.3发布,4.3将于12月终止维护

    10月27日,Spring Framework团队宣布了5.3版本正式GA,Spring用户可以在repo.spring.io和Maven Central上获取到最新版本的依赖包. JDK的版本支持 ...

  3. Jmeter 用户定义的变量的使用

    第一步: 打开Jmeter软件,新建一个线程组,添加 > 配置元素 > 用户定义的变量 第二步: 设置值,如下图所示: 第三步,使用设置的名称  :

  4. xadmin开发后台管理系统常见问题

    Xadmin开发后台管理系统 关注公众号"轻松学编程"了解更多. 添加小头像 https://blog.csdn.net/qq_34964399/article/details/8 ...

  5. python爬虫使用scrapy框架

    scrapy框架提升篇 关注公众号"轻松学编程"了解更多 1.创建启动爬虫脚本 在项目目录下创建start.py文件: 添加代码: #以后只要运行start.py就可以启动爬虫 i ...

  6. MeterSphere开发者手册

    什么是 MeterSphere MeterSphere 是一站式的开源企业级持续测试平台, 涵盖测试跟踪.接口测试.性能测试. 团队协作等功能,兼容 JMeter 等开源标准,有效助力开发和测试团队充 ...

  7. 浅谈MVP

    MVP是什么 MVP:Model-View-PresenterModel:表示数据提供者:View:表示数据展示:Presenter:是M与V沟通的桥梁. MVP工作方式 UI:告知Presenter ...

  8. How to resolve DynamicHeight problem in Morphx report[X++]

    For set dynamic height for controls in report on executeSection method: method 01 real maxHeight; st ...

  9. python数据分析02语法基础

    在我来看,没有必要为了数据分析而去精通Python.我鼓励你使用IPython shell和Jupyter试验示例代码,并学习不同类型.函数和方法的文档.虽然我已尽力让本书内容循序渐进,但读者偶尔仍会 ...

  10. AES的数学基础

    有限域 有限域上的运算 加法 两个多项式进行加法运算,就是两个多项式对应系数模2相加 乘法 两个多项式进行乘法运算:两个多项式相乘 若运算结果超过7次方,则必须对此结果进行一个多项式m(x)模运算,其 ...