SpringBoot助力!轻松实现微信模版消息推送

香编程~

加个“星标”,每日良时,好文必达呀!!

SpringBoot助力!轻松实现微信模版消息推送

本篇文章的主题是 如何通过springboot来实现 微信的模版消息推送

实现效果:

SpringBoot助力!轻松实现微信模版消息推送

在当今的信息化时代,微信作为国人最为常用的通讯工具之一,已经不仅仅是一个简单的社交应用,更是连接人与服务、人与信息的桥梁。企业微信模板消息作为一种高效、便捷的信息传递方式,被广泛应用于各类业务场景中,如订单通知、会议提醒、活动推送等。

通过本教程的学习,您将掌握如何在Spring Boot项目中集成微信SDK,如何编写代码发送微信模板消息,并了解整个推送的过程。

简要说明:由于发送模版消息需要微信的服务号,申请服务号的话需要营业执照,个人是没有办法申请的,但是微信为了给开发者们提供测试特意开放了公众平台测试账号号,大家可以申请测试号来进行模版推送的开发和测试

开发步骤:

  1. 访问微信公众平台测试账号页面

  2. 获取appID和appsecret

  3. 二维码添加测试号

  4. 添加模版消息

  5. 集成微信SDK

  6. 调用相关API

1、微信公众平台测试号管理

1.1 访问微信公众平台测试账号页面

大家首先百度访问或者搜索微信公众平台地址,如果还没有注册账号的可以申请一个个人订阅号,这个教程大家网上自行查阅,很简单~

SpringBoot助力!轻松实现微信模版消息推送

登录成功之后选择 开发者工具 –> 公众平台测试账号

SpringBoot助力!轻松实现微信模版消息推送

测试号管理页面如下:

SpringBoot助力!轻松实现微信模版消息推送

1.2 获取appID和appsecret

获取你的测试号信息

SpringBoot助力!轻松实现微信模版消息推送

1.3 扫二维码添加测试号

使用你的微信扫描这个测试公众号的二维码并关注然后会得到你的微信号(openId)这个后面会用到

SpringBoot助力!轻松实现微信模版消息推送

1.4 添加模版消息

点击新增测试模版

SpringBoot助力!轻松实现微信模版消息推送

添加模板信息一定要按照注意事项填写 参数需以{{开头,以.DATA}}结尾

SpringBoot助力!轻松实现微信模版消息推送

我这边创建了两个模版,这个模版id后面也会用到

SpringBoot助力!轻松实现微信模版消息推送

2、集成微信SDK

2.1 引入微信工具包

        <dependency>
           <groupId>com.github.binarywang</groupId>
           <artifactId>weixin-java-mp</artifactId>
           <version>3.0.0</version>
       </dependency>

2.2 添加配置文件

appId、appSecret和orderTemplateId就是上面微信公众平台测试号管理中我们获取到的几个参数,现在把这三个参数配置到我们的项目中。callBackUrl暂时先不用管

SpringBoot助力!轻松实现微信模版消息推送

创建配置类

@ConfigurationProperties(prefix = "wechat.public")
@Component
@Data
@RefreshScope
public class WeChatProperties {

   private String appId;

   private String appSecret;

   private String callBackUrl;

   private String orderTemplateId;
}

3、API调用

3.1 发送消息模版的实现

package com.mdx.user.manager;

import com.mdx.user.config.WeChatProperties;
import lombok.extern.slf4j.Slf4j;
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
* @author : jiagang
* @date : Created in 2022/7/26 10:42
*/
@Component
@Slf4j
public class WxMessagesManager {

   @Autowired
   private WeChatProperties weChatProperties;


   public void sendOrderMsg(String openId, String orderId, String serviceName){

       String templateId = weChatProperties.getOrderTemplateId();

       // 订单时间
       SimpleDateFormat sdf = new SimpleDateFormat();
       sdf.applyPattern("yyyy-MM-dd HH:mm");
       Date date = new Date();
       String timeNow = sdf.format(date);

       WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
       wxStorage.setAppId(weChatProperties.getAppId());
       wxStorage.setSecret(weChatProperties.getAppSecret());

       WxMpService wxMpService = new WxMpServiceImpl();
       wxMpService.setWxMpConfigStorage(wxStorage);
       // 此处的 key/value 需和模板消息对应
       List<WxMpTemplateData> wxMpTemplateDataList = Arrays.asList(
               new WxMpTemplateData("first", "您有一个新的订货单", "#FF0000"),
               new WxMpTemplateData("keyword1", orderId),
               new WxMpTemplateData("keyword2", serviceName),
               new WxMpTemplateData("keyword3", timeNow),
               new WxMpTemplateData("remark", "请登录系统查看订单详情并及时配货")
      );

       WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
              .toUser(openId) // openId为1.3步骤中得到的微信号
              .templateId(templateId)
              .data(wxMpTemplateDataList)
              .url("https://blog.csdn.net/qq_38374397?type=blog")  // 跳转详情地址
              .build();

       try {
           wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
           log.info("消息模版发送成功~");
      } catch (Exception e) {
           log.error("推送失败:" + e.getMessage());
      }

  }
}

3.2 测试类调用

openId为1.3步骤中得到的微信号,其余参数可自定义

SpringBoot助力!轻松实现微信模版消息推送

SpringBoot助力!轻松实现微信模版消息推送

3.3 效果展示

移动端:

SpringBoot助力!轻松实现微信模版消息推送

点击详情将调整到我们预先配置的详情页面,我的详情页如下:


SpringBoot助力!轻松实现微信模版消息推送



PC端:

SpringBoot助力!轻松实现微信模版消息推送


本篇文章到这里就结束了,最后送大家一句话 白驹过隙,沧海桑田

END


SpringBoot助力!轻松实现微信模版消息推送


PS:防止找不到本篇文章,可以收藏点赞,方便翻阅查找哦。

原文始发于微信公众号(迷迭香编程):SpringBoot助力!轻松实现微信模版消息推送

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/207638.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!