添加依赖

在SpringBoot项目中添加依赖

<!--微信模版消息推送三方sdk--><dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-mp</artifactId><version>3.3.0</version></dependency>

控制层代码

package com.laoxu.demo.bootstrapcurd.controller;import com.laoxu.demo.bootstrapcurd.service.message.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.io.IOException;/*** @author: wangxiaobo* @create: 2022-08-31 21:03* 模板微信推送消息处理接口**/
@RestController
@RequestMapping("/v1/weChar")
public class TemplateController {@Autowiredprivate TemplateService templateService;/*** 设置行业信息* @param body 请求对象* @return* @throws IOException*/@RequestMapping("/setIndustry")public String setIndustry(@RequestBody String body) throws IOException {return templateService.setIndustry(body);}/*** 获取行业信息* @return* @throws IOException*/@RequestMapping("/getIndustry")public String setIndustry() throws IOException {return templateService.getIndustry();}/*** 获取微信公众平台全部模版消息* @return* @throws IOException*/@RequestMapping("/getTemplateList")public String getTemplateList() throws IOException {return templateService.getTemplateList();}/*** 发送模版消息* @param body* @return* @throws IOException*/@RequestMapping("/send")public String send(@RequestBody String body) throws IOException {return templateService.send(body);}}

去微信公众平台注册一个开发测试账号

个人开发,我们可以去微信公众号平台注册个测试账户点我直达,微信扫码登录,会给我们一个免费的:appID、appsecret,微信扫码关注公众号,会显示关注测试公众号的用户列表。全局错误码:点我直达

测试

关注测试公众号,创建模板,并发送指定模板内容

 替换版本内容

在微信公众平台创建模板
语法:{{变量名.DATA}}
姓名:{{user_name.DATA}}
性别:{{sex.DATA}}
手机号:{{phone.DATA}}
邮箱:{{email.DATA}}

模板消息处理服务类代码

package com.laoxu.demo.bootstrapcurd.service.message;import com.laoxu.demo.bootstrapcurd.bean.HttpClientUtils;
import com.laoxu.demo.bootstrapcurd.constant.WeCharConstant;
import com.laoxu.demo.bootstrapcurd.service.AccessTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.io.IOException;/*** @author: wangxiaobo* @create: 2022-08-31 20:59* 模板消息处理服务类**/
@Service
public class TemplateService {@Autowiredprivate AccessTokenService accessTokenService;/*** 设置行业信息** @param body 请求对象* @return* @throws IOException*/public String setIndustry(String body) throws IOException {return HttpClientUtils.post(WeCharConstant.SET_INDUSTRY.replace("ACCESS_TOKEN", accessTokenService.getAccessToken()),body);}/*** 获取行业信息** @return* @throws IOException*/public String getIndustry() throws IOException {return HttpClientUtils.get(WeCharConstant.GET_INDUSTRY.replace("ACCESS_TOKEN", accessTokenService.getAccessToken()));}/*** 获取微信公众平台全部模版消息** @return* @throws IOException*/public String getTemplateList() throws IOException {return HttpClientUtils.get(WeCharConstant.GET_ALL_PRIVATE_TEMPLATE.replace("ACCESS_TOKEN", accessTokenService.getAccessToken()));}/*** 发送模版消息** @param body 请求数据* @return* @throws IOException*/public String send(String body) throws IOException {return HttpClientUtils.post(WeCharConstant.TEMPLATE_SEND.replace("ACCESS_TOKEN", accessTokenService.getAccessToken()), body);}}

获取微信Access token用来做获取其他接口验证鉴权

AccessTokenService.java

获取Access token服务层
package com.laoxu.demo.bootstrapcurd.service;import com.alibaba.fastjson.JSONObject;
import com.laoxu.demo.bootstrapcurd.bean.AccessTokenBean;
import com.laoxu.demo.bootstrapcurd.bean.HttpClientUtils;
import com.laoxu.demo.bootstrapcurd.config.WeCharConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.io.IOException;/*** @author: wangxiaobo* @create: 2022-08-29 13:15* 获取Access token服务层**/
@Service
public class AccessTokenService {@Autowiredprivate WeCharConfig weCharConfig;/*** access Token对象*/private AccessTokenBean accessTokenBean;private static final String URL="https://api.weixin.qq.com/cgi-bin/token";/*** 获取 access Token* @param appId* @param appSecret* @throws Exception*/private void refreshAccessToken(String appId, String appSecret) throws IOException {String s = HttpClientUtils.get(URL,AccessTokenBean.requesOf (appId,appSecret));JSONObject jsonObject = JSONObject.parseObject (s);long expiresIn = jsonObject.getLong("expires_in");String accessToken = jsonObject.getString("access_token");accessTokenBean = AccessTokenBean.responseOf(accessToken,expiresIn);System.out.println(s);}/*** 获取 access Token* @return*/public String getAccessToken() throws IOException {if(accessTokenBean==null || accessTokenBean.isExpired()){refreshAccessToken(weCharConfig.getAppId (),weCharConfig.getSecret());}return accessTokenBean.getAccess_token ();}public static void main(String[] args) throws IOException {AccessTokenService accessTokenService = new AccessTokenService ();accessTokenService.refreshAccessToken("wxc241d23b510947a4","5cfef2542ececfae0357f3f125e39f17");}
}

AccessTokenBean.Java

access token对象

package com.laoxu.demo.bootstrapcurd.bean;import lombok.Getter;import java.io.Serializable;
import java.util.HashMap;/*** access token对象* @author: wangxiaobo* @create: 2022-08-29 12:51***/
@Getter
public class AccessTokenBean implements Serializable {/*** 获取access_token接口对象参数*//*** 获取access_token填写client_credential*/private String grant_type="client_credential";/*** 第三方用户唯一凭证*/private String appid;/*** 第三方用户唯一凭证密钥,即appsecret*/private String secret;/*** 返回参数说明*//*** access_token获取到的凭证*/private String access_token;/*** 凭证有效时间,单位:秒*/private long expires_in;public AccessTokenBean( String appid, String secret, String access_token, long expires_in) {this.appid = appid;this.secret = secret;this.access_token = access_token;//这样就拿到过期失效时间if (expires_in > 0){this.expires_in = System.currentTimeMillis() + expires_in * 1000;}this.expires_in = expires_in;}/*** 构建获取 access_token请求对象* @param appid 用户唯一标识* @param secret 用户唯一标识密钥* @return*/public static HashMap<String,String> requesOf(String appid, String secret){HashMap<String, String> map = new HashMap<> (16);map.put("appid",appid);map.put("secret",secret);map.put("grant_type","client_credential");return map;}/*** 构建access_token对象* @param access_token token* @param expires_in  过期时间* @return*/public static AccessTokenBean responseOf(String access_token, long expires_in){return new AccessTokenBean ("","", access_token,expires_in);}/*** 判断token是否已经失效* @return 是否失效 false 未失效 true 失效,需要重新申请*/public boolean isExpired(){return System.currentTimeMillis ()>expires_in;}
}

import lombok.Data;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;/*** @author: wangxiaobo* @create: 2022-08-29 13:17**/
@Component
@Data
@ConfigurationProperties(prefix = "we-char")
@Getter
public class WeCharConfig {/*** 第三方用户唯一凭证*/private String appId;/*** 第三方用户唯一凭证密钥,即appsecret*/private String secret;/*** 加密token*/private String token;}

测试获取微信Access token接口

package com.laoxu.demo.bootstrapcurd.controller;import com.laoxu.demo.bootstrapcurd.service.AccessTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author: wangxiaobo* @create: 2022-08-29 13:56**/
@RestController
@RequestMapping("/accessToken")
public class AccessTokenController {@Autowiredprivate AccessTokenService accessTokenService;/*** 获取微信Access token接口* @return* @throws Exception*/@RequestMappingpublic String getAccessToken() throws Exception {return accessTokenService.getAccessToken();}}

access_token:接口调用凭证

expires_in:过期时间

{"access_token":"60_A8T9k5Plmyxkc6PG5dHYXhRsBvb9Ce1HN67a9awvHRWsS7nxWwKWuR6kU1G_yyINRJLPiEoluHKoGKqWvwls2wbz32tvxr4UdW2f_bqX4camjhuDXCkL-IlYZbukpc9zjaBjHACElJTokqflJVMjABAYKB","expires_in":7200}

微信公众平台常量类

package com.laoxu.demo.bootstrapcurd.constant;/*** @author: wangxiaobo* @create: 2022-08-29 22:09* 微信公众平台常量类**/
public class WeCharConstant {public static final String CREATE_TIME = "CreateTime";public static final String CONTENT = "Content";public static final String FROM_USER_NAME = "FromUserName";public static final String TO_USER_NAME = "ToUserName";public static final String MSG_TYPE = "MsgType";public static final String MSG_ID = "MsgId";/*** 创建菜单URL*/public static final String CREATE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";/*** 删除菜单URL*/public static final String DELETE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN";/*** 模版服务相关-设置所属行业*/public static final String SET_INDUSTRY = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=ACCESS_TOKEN";/*** 获取所属行业信息*/public static final String GET_INDUSTRY = "https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=ACCESS_TOKEN";/*** 获取所有模版信息*/public static final String GET_ALL_PRIVATE_TEMPLATE = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=ACCESS_TOKEN";/*** 发送模版消息*/public static final String TEMPLATE_SEND = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";/*** 获取OAUTH认证的access_token*/public static final String OAUTH_GET_AT = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";/*** 获取用户基本信息*/public static final String OAUTH_USER_INFO = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";/*** 引导链接*/public static final String OAUTH2_AUTHORIZE = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";}

接下来就是微信测试模板消息推送

package com.laoxu.demo.bootstrapcurd.controller;import com.laoxu.demo.bootstrapcurd.service.message.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.io.IOException;/*** @author: wangxiaobo* @create: 2022-08-31 21:03* 模板微信推送消息处理接口**/
@RestController
@RequestMapping("/v1/weChar")
public class TemplateController {@Autowiredprivate TemplateService templateService;/*** 设置行业信息* @param body 请求对象* @return* @throws IOException*/@RequestMapping("/setIndustry")public String setIndustry(@RequestBody String body) throws IOException {return templateService.setIndustry(body);}/*** 获取行业信息* @return* @throws IOException*/@RequestMapping("/getIndustry")public String setIndustry() throws IOException {return templateService.getIndustry();}/*** 获取微信公众平台全部模版消息* @return* @throws IOException*/@RequestMapping("/getTemplateList")public String getTemplateList() throws IOException {return templateService.getTemplateList();}/*** 发送模版消息* @param body* @return* @throws IOException*/@RequestMapping("/send")public String send(@RequestBody String body) throws IOException {return templateService.send(body);}}

测试发送模板接口之前首先要获取模板ID:template_id

 测试发送模板接口

touser:填你的openId就是扫测试号的微信号

template_id:模板ID

url:要跳转的链接

{"touser":"owfI16osHtU_UV277BYfm0EwPJPo","template_id":"yuaFG-DmpAZiCM22aBQ9we3Luk8g6Xv400qQXcfn84g","url":"http://www.baidu.com",  "data":{"first": {"value":"中国移动通信缴费通知","color":"#173177"},"keyword1":{"value":"填写手机号"},"keyword2": {"value":"-60元","color":"#FF0000"},"keyword3": {"value":"60元","color":"#173177"},"keyword4": {"value":"2022年8月31日","color":"#173177"},"keyword5": {"value":"666666666","color":"#173177"},"remark":{"value":"欢迎使用中国移动通信","color":"#173177"}}}

接受推送消息

SpringBoot 实现微信模板消息通知推送提醒相关推荐

  1. springboot实现微信模板消息推送

    springboot实现微信模板消息推送 在上一篇文章我们已经知道了怎么获取openid 还不知道的可以查看我的上一篇文章springboot+微信小程序用codeid换取openid 这次我们不光要 ...

  2. 突破微信小程序模板消息的推送限制

    "模版消息"是小程序非常重要且可主动触达用户的一种能力.爱鲜蜂小程序通过"模版消息",建立一套用户唤醒机制,达到提升用户复购率的目的.小打卡小程序的近30天访问 ...

  3. 群晖消息通知 推送服务器,群晖开启系统信息微信推送服务

    1.在电脑上,用浏览器到大名鼎鼎的GitHub注册一个账号,地址:https://github.com/join?source=login  ,注册过程略过,注册好账号以后登录该网站(如果之前已经注册 ...

  4. openfire消息通知推送_微信小游戏内测「订阅消息」能力,这是召回用户的「大杀器」吗?...

    作者:蒋鸿昌 本文来源于「知晓程序」公众号.知晓云后端云服务,让你的小程序开发快人一步,添加「minsupport3」了解详情. 知晓云​cloud.minapp.com 一位投资人曾把最近 3 年微 ...

  5. php微信公众号模板消息主动推送

    1.获取access_token,有效期7200秒,我的方法是记录获取时间,超过时间再次获取. 提供:appid ,appsec public function get_token(){$m = ne ...

  6. openfire消息通知推送_APP消息推送功能之前端后台设计

    APP消息推送功能之前端后台设计 最近有不少小伙伴问APP消息推送功能,前端.后台如何设计的?消息系统的架构是什么样的?最近刚好做到后台消息推送这块,简单谈谈个人心得,欢迎拍砖. 消息推送是让自己的用 ...

  7. SpringBoot 集成 WebSocket 实现消息群发推送

    一. 什么是 WebSocket WebSocket 是一种全新的协议.它将 TCP 的 Socket(套接字)应用在了web page上,从而使通信双方建立起一个保持在活动状态的连接通道,并且属于全 ...

  8. iOS项目开发实战——实现苹果本地消息通知推送服务

    当你一个App在后台运行时,有可能服务器会向你推送重要的信息,常见的如微信,QQ等,就算你的App在后台,也会以通知的形式给你推送.推送服务分为本地推送和在线推送.本次我们先来实现本地推送通知. (1 ...

  9. php通告信息显示模板,微信模板消息通知方法

    用于3.3.1以上版本 我们将介绍如何开发模板消息,即如何用程序发送模板消息功能.本文分为以下三个部分:申请模板消息权限 开发模板消息SDK 构造模板消息体并发送 一.申请模板消息权限 模板消息的申请 ...

最新文章

  1. 利用Cache,asp.net 简单实现定时执行任务
  2. char (*p3)[5] = a; 和char (*p4)[5] = a;的区别?
  3. Redis持久化和备份数据
  4. 微信小程序安卓机使用uploadfile提示undefined错误原因
  5. 高等数学:第十一章 无穷级数(3)正弦级数、余弦级数、周期为2L的周期函数的傅里叶级数
  6. 1.11 多异常捕获
  7. ANSYS——查看某一截面的云图分布(也叫做切片图)
  8. java与java ee_Java EE 8 MVC:全局异常处理
  9. 设计模式 - Command
  10. 开源大数据:MLSQL
  11. 迁移学习笔记1:简明手册笔记
  12. 人脸识别技术大起底,你了解多少?
  13. cnpack多国语言控件帮助
  14. 数理统计与数据分析第三版习题 第3章 第33-35题
  15. 【Tensorflow 报错】struct.error: 'i' format requires -2147483648 = number = 2147483647
  16. javascript如何获取request中的数据
  17. Javascript笔记大全01,会持续更新~
  18. 最新kali linux下完美安装和运行QQ的方法
  19. AntiVir德国小红伞杀毒使用
  20. 那年冬天风在吹宋慧乔win7主题

热门文章

  1. 对list集合重新排序
  2. 多无人机辅助移动边缘计算中的任务卸载和轨迹优化
  3. 关于福昕软件公司的控件使用-Foxit PDF SDK ActiveX
  4. 转:黑阔之超级约会学
  5. [转]般若波罗密多心经
  6. 关于数据库中的schema的注释
  7. js中function和Function的区别
  8. Hibernate学习大全
  9. 2021-03-23 - 高性能 Redis 实战
  10. 设计模式系列-Builder模式(高效构建参数)