导读

  由于最近手头上需要做个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. 从小白到 6 个 offer,我究竟是怎么刷题的?

    最近自习室里又兴起了一阵刷题潮,大家相约刷题~ 今天和大家系统分享下我去年转行时的一个刷题过程和方法,希望对你有所帮助. 首先介绍下我的编程基础,我学的是金融工程专业,硕士时学过 C++ 的课,这也是 ...

  2. Java的类加载器有几种?什么是双亲委派机制?

    一.JAVA类加载器包括几种? 启动类加载器 bootstrap class loader 启动类加载器主要加载的是JVM自身需要的类,这个类加载是用C++语言实现的,是虚拟机自身的一部分,它负责将 ...

  3. leetcode95:jump game

    题目描述 给出一个非负整数数组,你最初在数组第一个元素的位置 数组中的元素代表你在这个位置可以跳跃的最大长度 判断你是否能到达数组最后一个元素的位置 例如 A =[2,3,1,1,4], 返回 tru ...

  4. Python 3.9就要来了......,令人兴奋的时刻

    本文主要介绍Python3.9的一些新特性,如:更快速的进程释放,性能的提升,简便的新字符串函数,字典并集运算符以及更兼容稳定的内部API,详细如下: 字典并集和可迭代更新 字符串方法 类型提示 新的 ...

  5. 论文阅读:Learning Attention-based Embeddings for Relation Prediction in Knowledge Graphs(2019 ACL)

    基于Attention的知识图谱关系预测 论文地址 Abstract 关于知识库完成的研究(也称为关系预测)的任务越来越受关注.多项最新研究表明,基于卷积神经网络(CNN)的模型会生成更丰富,更具表达 ...

  6. Docker学习—Machine

    前言 前面<Docker学习-Compose>文中介绍了Compose的使用方式:接下来继续了解docker三剑客之一的 Machine: 一.Docker Machine简介 1.什么是 ...

  7. 1. 线性DP 1143. 最长公共子序列

    最经典双串: 1143. 最长公共子序列 (LCS)  https://leetcode-cn.com/problems/longest-common-subsequence/submissions/ ...

  8. uboot——do_bootm

    do_bootm |----------根据参数得到 image的起始地址 |----------比较header的 magic_num 是否为 zImage | |是 | | zImage路线 | ...

  9. 利用移动硬盘安装windows7系统

    首先把win7系统镜像的iso文件解压到移动硬盘中 将移动硬盘设置为活动分区 设置活动分区的方法 Diskpart程序实现U盘安装WIN7的方法: 将Win7安装盘中的所有文件拷贝到硬盘文件夹中,我们 ...

  10. 2020年最新ZooKeeper面试题(附答案)

    2020年最新ZooKeeper面试题 1. ZooKeeper 是什么? ZooKeeper 是一个开源的分布式协调服务.它是一个为分布式应用提供一致性服务的软件,分布式应用程序可以基于 Zooke ...