java向微信公众号 | 您所在的位置:网站首页 › 图文推送模板 › java向微信公众号 |
微信公众号初次开发
其他操作
项目搭建mavenapplication.ymlyml参数配置微信客户端配置
搭建完成 实现业务模板消息推送准备工作模板消息填写要求图文消息填写要求
推送模板消息推送模板消息结果——效果
推送图文消息推送图文消息工具类问题和结果
获取关注的用户openId既然有了openId集合和要发送的消息怎么群发
其他操作
书接上文:微信测试号申请步骤和调试. 书接下文:后台获取授权用户的openId. 项目搭建 maven commons-codec commons-codec 1.13 org.jolokia jolokia-core 1.6.0 net.coobird thumbnailator 0.4.8 com.github.binarywang weixin-java-mp 3.6.0 org.springframework.boot spring-boot-starter org.projectlombok lombok true com.alibaba druid 1.1.16 com.alibaba druid-spring-boot-starter 1.1.10 org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-test test org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.0 mysql mysql-connector-java 5.1.47 org.springframework.boot spring-boot-starter-web com.alibaba fastjson 1.2.61 org.apache.commons commons-lang3 3.8.1 org.mybatis.spring.boot mybatis-spring-boot-autoconfigure 1.3.2 com.squareup.okhttp3 okhttp com.baomidou mybatis-plus 3.1.2 io.springfox springfox-swagger2 2.7.0 io.springfox springfox-swagger-ui 2.7.0 javax.persistence persistence-api 1.0 com.github.pagehelper pagehelper-spring-boot-starter 1.2.9 application.yml ################### 项目启动端口 ################### server: port: 80 # 微信公众号配置 wx: #微信公众号APPID appid: 123 #微信公众号SECRET secret: 123 #微信公众号模板ID templateId: 123 yml参数配置 import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * 微信参数 **/ @Data @Component @ConfigurationProperties(prefix = "wx") public class WxMpProperties { /** * 公众号appId */ private String appId; /** * 公众号appSecret */ private String secret; /** * 公众号模板ID */ private String templateId; } 微信客户端配置 import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; import me.chanjar.weixin.mp.config.WxMpConfigStorage; import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 微信配置 **/ @Configuration public class WxConfig { @Autowired WxMpProperties wxMpProperties; /** * 构造注入 * * @param wxMpProperties */ WxConfig(WxMpProperties wxMpProperties) { this.wxMpProperties = wxMpProperties; } /** * 微信客户端配置存储 * * @return */ @Bean public WxMpConfigStorage wxMpConfigStorage() { WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl(); // 公众号appId configStorage.setAppId(wxMpProperties.getAppId()); // 公众号appSecret configStorage.setSecret(wxMpProperties.getSecret()); // 公众号Token configStorage.setToken(wxMpProperties.getToken()); // 公众号EncodingAESKey configStorage.setAesKey(wxMpProperties.getAesKey()); return configStorage; } /** * 声明实例 * * @return */ @Bean public WxMpService wxMpService() { WxMpService wxMpService = new WxMpServiceImpl(); wxMpService.setWxMpConfigStorage(wxMpConfigStorage()); return wxMpService; } } 搭建完成 实现业务 模板消息推送 准备工作
first.DATA 跟上.DATA说明是数据 人员:{{first.DATA}} 地址:{{keyword1.DATA}} 区域:{{keyword2.DATA}} 告警类型:{{remark1.DATA}} 时间:{{remark2.DATA}} 图文消息填写要求 推送模板消息这里使用微信提供的serviceApi 链接: WxMpService. import com.shangqu.datapush.buildsite.model.emnus.AlarmType; import com.shangqu.datapush.buildsite.model.mysqlData.AlarmInfo; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.mp.api.WxMpAiOpenService; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpUserService; import me.chanjar.weixin.mp.bean.result.WxMpUserList; import me.chanjar.weixin.mp.bean.template.WxMpTemplateData; import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PostMapping; /** * 微信消息推送 * **/ @Slf4j @Component @CrossOrigin public class WxMsgPush { /** * 微信公众号API的Service */ private final WxMpService wxMpService; /** * 构造注入 */ WxMsgPush(WxMpService wxMpService) { this.wxMpService = wxMpService; } /** * 发送微信模板信息 * * @param openId 接受者openId * @return 是否推送成功 */ public Boolean SendWxMsg(String openId,String templateId) { // 发送模板消息接口 WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder() // 接收者openid .toUser(openId) // 模板id .templateId(templateId) // 模板跳转链接 .url("www.baidu.com") .build(); // 添加模板数据 templateMessage.addData(new WxMpTemplateData("first", "null告警""#000000")) .addData(new WxMpTemplateData("keyword1", "","#000000")) .addData(new WxMpTemplateData("keyword2","", "#000000")) .addData(new WxMpTemplateData("keyword3", "", "#000000")) .addData(new WxMpTemplateData("keyword4", "", "#000000")); String msgId = null; try { // 发送模板消息 msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage); } catch (WxErrorException e) { e.printStackTrace(); } log.warn("·==++--·推送微信模板信息:{}·--++==·", msgId != null ? "成功" : "失败"); return msgId != null; } } 推送模板消息结果——效果
要用户先发送一条消息给公众号,你才能发送成功 微信并没有提供群发接口–就在自己代码中迭代openId集合去循环发送 |
CopyRight 2018-2019 实验室设备网 版权所有 |