Java实现微信公众号发送通知

简介: Java实现微信公众号发送通知

准备工作

 1.登录微信公众平台进入自己的微信公众号点击广告与服务下的订阅通知,如下图

image.gif 编辑

2.点击公共模板库,选择一个自己想要的模板(模板只能使用微信提供的模板),按照自己的需求去找一个模板,我这里要做一个访客出场提示。

image.gif 编辑

3.点击选用,保留自己想要的提示项即可。

image.gif 编辑

4.点击提交之后即可生成一条模板信息

image.gif 编辑

Java代码

1.在yaml文件里配置模板id

image.gif 编辑

2.编写发送微信通知接口

SocialClientService:

void sendWxMPMessage(String openId, String templateId, String page, Map<String, String> dataMap) throws Exception;

image.gif

参数为:openId,即唯一id,templatedId就是模板id,page可以为null,dataMap就是拼凑好的数据Map集合

实现类SocialClientServiceImpl:

@Override
    public void sendWxMPMessage(String openId, String templateId, String page, Map<String, String> dataMap) throws Exception {
        WxMpService wxMpService1 = wxMpServiceCache.get(clientIdMP + ":" + clientSecretMP);
        WxMpSubscribeMessage wxMpSubscribeMessage = WxMpSubscribeMessage.builder()
                .toUser(openId)
                .templateId(templateId)
                .url(page)
                .dataMap(dataMap)
                .build();wxMpService1.getTemplateMsgService().getAllPrivateTemplate();
        wxMpService1.getSubscribeMsgService().send(wxMpSubscribeMessage);
    }

image.gif

3.调用接口

注入yaml里面配置好的通知模板id:

@Value("${wx.visitor-template-id}")

   private String visitorTemplateId;

注意每项字数不得超过20个字。

private void sendWxMessage(VisitorManageDO visitorManage) {
        //发送微信通知
        Map<String,String> data = new HashMap<>();
        data.put("name1", visitorManage.getVisitorName());
        data.put("thing4", "感谢您访问***,请您在半个小时内离开厂区,谢谢配合,再见");
        log.info("send-mp-visitor,id===>{},data===>{}",visitorManage.getId() , data);
        try {
            socialClientService.sendWxMPMessage(visitorManage.getOpenId(), visitorTemplateId, null, data);
            log.info("send-mp-visitor-success,id===>{}",visitorManage.getId());
        } catch (Exception e) {
            log.error("send-mp-visitor-error,id===>{},msg===>{}",visitorManage.getId(),e.getMessage());
        }
    }

image.gif

前端预约提示框代码:

<wx-open-subscribe
    class="custom-submit-btn"
    id="subscribe-btn"
    template="f33bI1AS9IC5-eiUW0iSnyCm_hm1Xb6Bf,hs6qtokIu1T9oxpPgU08QEY5V4"--模板id,多个模板id用逗号隔开 
      @success="onSubscribeSuccess"
      @error="onSubscribeError"
    @cancel="handleSubscribeCancel"
    >
  <component is="script" type="text/wxtag-template" slot="style">
    <component is="style">
      .subscribe-btn {
       background: #007aff !important; 
      color: #fff !important;
    font-size: 18px !important;
    border-radius:10px;
    line-height: -28px !important;
    text-align: center !important;
    margin: 0px 0 0 33px !important;
      }
    </component>
  </component>
      <component is="script" type="text/wxtag-template">
    <button class="subscribe-btn">
      提交             
    </button>
      </component>
    </wx-open-subscribe>

image.gif

image.gif 编辑

如下图所示,因为我们申请的是一次性模板,所有我们点击允许通知才能接收到一条消息,所以需要在前端显示多个是否允许多个模板通知。

image.gif 编辑


目录
相关文章
|
JSON 小程序 Java
微信公众平台测试号——模板消息发送Demo
微信公众平台测试号——模板消息发送Demo
2532 0
|
Java 关系型数据库 数据库连接
【SpringBoot】入门到精通 这一篇就够了
【SpringBoot】入门到精通 这一篇就够了
789 1
|
7月前
|
中间件 测试技术 API
值得收藏,一些好用的Claude Code提示词!
值得收藏,一些好用的Claude Code提示词!
|
前端开发 数据处理
芋道审批流配置流程表单超详细介绍
芋道审批流配置流程表单超详细介绍
2519 63
|
人工智能 Java API
Java AI智能体实战:使用LangChain4j构建能使用工具的AI助手
随着AI技术的发展,AI智能体(Agent)能够通过使用工具来执行复杂任务,从而大幅扩展其能力边界。本文介绍如何在Java中使用LangChain4j框架构建一个能够使用外部工具的AI智能体。我们将通过一个具体示例——一个能获取天气信息和执行数学计算的AI助手,详细讲解如何定义工具、创建智能体并处理执行流程。本文包含完整的代码示例和架构说明,帮助Java开发者快速上手AI智能体的开发。
3852 8
|
存储 人工智能 Java
Spring AI与DeepSeek实战四:系统API调用
在AI应用开发中,工具调用是增强大模型能力的核心技术,通过让模型与外部API或工具交互,可实现实时信息检索(如天气查询、新闻获取)、系统操作(如创建任务、发送邮件)等功能;本文结合Spring AI与大模型,演示如何通过Tool Calling实现系统API调用,同时处理多轮对话中的会话记忆。
3198 57
|
SQL 关系型数据库 MySQL
MySQL 8.0报错--1118-Row size too large. The maximum row size for the used table type, not counting BLOBs,is 8126,
MySQL 8.0报错--1118-Row size too large. The maximum row size for the used table type, not counting BLOBs,is 8126,
2726 56
MySQL 8.0报错--1118-Row size too large. The maximum row size for the used table type, not counting BLOBs,is 8126,
|
存储 NoSQL Redis
redis-(error)-MISCONF。Redis。is。configuredto。save。RDBsnapshots
redis-(error)-MISCONF。Redis。is。configuredto。save。RDBsnapshots
3313 0
|
人工智能 Java API
Java 也能快速搭建 AI 应用?一文带你玩转 Spring AI 可观测性
Java 也能快速搭建 AI 应用?一文带你玩转 Spring AI 可观测性
|
SQL 前端开发 关系型数据库
芋道框架万字详解(前后端分离)、若依框架、yudao-cloud保姆级攻略
芋道框架万字详解(前后端分离)、若依框架、yudao-cloud保姆级攻略
25139 7

热门文章

最新文章