002java实现微信模版消息推送
本节知识点
1,注册微信公号测试账号2,获取测试账号的用户openid3,接入微信推送sdk4,实现微信推送课程中用到的网址和文件
1,微信官方注册测试账号:https://mp./debug/cgi-bin/sandbox?t=sandbox/login配套笔记
5行代码实现微信模版消息推送
视频地址
在线视频:/course/detail/23750
核心代码
1 三方类库<!--微信模版消息推送三方sdk--><dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-mp</artifactId><version>3.3.0</version></dependency>
2 推送的核心类
public String push() {//1,配置WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();wxStorage.setAppId("wx77bb69292323a000");//appidwxStorage.setSecret("29bd368145806115ad683e62806e");//appsecretWxMpService wxMpService = new WxMpServiceImpl();wxMpService.setWxMpConfigStorage(wxStorage);//2,推送消息WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder().toUser("o5kho6DgC7SDry8zCmXuvHJGvrgI")//要推送的用户openid.templateId("Tpln-Eue2obJ0B-8JNkgkiRJaDMPgVeIgGxna982xrg")//模版id.url("/")//点击模版消息要访问的网址.build();//3,发起推送try {String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);System.out.println("推送成功:" + msg);return "推送成功:" + msg;} catch (Exception e) {System.out.println("推送失败:" + e.getMessage());e.printStackTrace();}return "推送失败";}
003java实现小程序模版消息推送
本节知识点
1,springboot创建小程序推送后台2,微信小程序开发的学习3,获取推送需要的formid4,实现微信小程序推送功能课程中用到的网址和文件
1,小程序学习视频:/course/detail/95312,小程序云开发获取用户openid:/course/play/9604/2045293,微信小程序官方推送文档:
https://developers./miniprogram/dev/framework/open-ability/template-message.html
配套笔记
5行代码实现微信小程序模版消息推送 (含推送后台和小程序源码)/p/35da86f309d4
视频地址
在线视频:/course/detail/23750
推送验证
http://localhost:8080/push?openid=o3DoL0WEdzcJ20AVJg1crP96gbjM&formid=00869c94008b43379e52658997d887e9
注意:这里一定要替换成你自己的openid,和formid。否则没法推送成功的
核心代码
1 三方类库<!--微信小程序模版推送--><dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-miniapp</artifactId><version>3.4.0</version></dependency>
2 推送的核心类
/*** Created by qcl on -05-20* 微信:2501902696* desc: 微信小程序模版推送实现*/@RestControllerpublic class PushController {@GetMapping("/push")public String push(@RequestParam String openid, @RequestParam String formid) {//1,配置小程序信息WxMaInMemoryConfig wxConfig = new WxMaInMemoryConfig();wxConfig.setAppid("wx7c54942dfc87f4d8");//小程序appidwxConfig.setSecret("5873a729c365b65ab42bb5fc82d2ed49");//小程序AppSecretWxMaService wxMaService = new WxMaServiceImpl();wxMaService.setWxMaConfig(wxConfig);//2,设置模版信息(keyword1:类型,keyword2:内容)List<WxMaTemplateData> templateDataList = new ArrayList<>(2);WxMaTemplateData data1 = new WxMaTemplateData("keyword1", "获取老师微信");WxMaTemplateData data2 = new WxMaTemplateData("keyword2", "2501902696");templateDataList.add(data1);templateDataList.add(data2);//3,设置推送消息WxMaTemplateMessage templateMessage = WxMaTemplateMessage.builder().toUser(openid)//要推送的用户openid.formId(formid)//收集到的formid.templateId("eDZCu__qIz64Xx19dAoKg0Taf5AAoDmhUHprF6CAd4A")//推送的模版id(在小程序后台设置).data(templateDataList)//模版信息.page("pages/index/index")//要跳转到小程序那个页面.build();//4,发起推送try {wxMaService.getMsgService().sendTemplateMsg(templateMessage);} catch (WxErrorException e) {System.out.println("推送失败:" + e.getMessage());return e.getMessage();}return "推送成功";}}
3,小程序获取formid的核心布局
<form class="form_item" bindsubmit='gorRunnerLobby' report-submit='true' data-type="1"><button class="button" form-type='submit'>获取formid</button></form>
4,小程序获取formid的核心js代码
//获取用户的formid,用于模版消息推送gorRunnerLobby(event) {console.log("formid: " + event.detail.formId);this.setData({formid: event.detail.formId})},