首页 » Web前端 » php模版推送办法技巧_5行代码实现微信模版消息推送java实现微信推送

php模版推送办法技巧_5行代码实现微信模版消息推送java实现微信推送

duote123 2024-12-10 0

扫一扫用手机浏览

文章目录 [+]

先看效果图:

核心代码只有下面几行,即可轻松实现微信模版推送

php模版推送办法技巧_5行代码实现微信模版消息推送java实现微信推送

//1,配置WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();wxStorage.setAppId(\公众wx77bb69292323a000\公众);wxStorage.setSecret(\"大众29bd368145806115ad6820133e62806e\"大众);WxMpService wxMpService = new WxMpServiceImpl();wxMpService.setWxMpConfigStorage(wxStorage);//2,推送WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder().toUser(\公众o5kho6DgC7SDry8zCmXuvHJGvrgI\"大众)//要推送的用户openid.templateId(\"大众Tpln-Eue2obJ0B-8JNkgkiRJaDMPgVeIgGxna982xrg\公众)//模版id.url(\公众https://30paotui.com/\公众)//点击模版要访问的网址.build();try { wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);} catch (Exception e) { System.out.println(\公众推送失落败:\"大众 + e.getMessage());}

所用知识点

php模版推送办法技巧_5行代码实现微信模版消息推送java实现微信推送
(图片来自网络侵删)
1, springboot实现java后台2,微信测试账号的申请3,微信模版推送的配置接下来就带领大家来一步步实现微信模版推送。

一,springboot创建java后台

至于springboot怎么创建java后台,我这里就不再唠叨了,大家百度一下,一大堆的文章。
这里只须要重点讲解下以下几点。

1,在pom.xml文件里引入下面类库

<!--微信模版推送三方sdk--><dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-mp</artifactId><version>3.3.0</version></dependency>2,写一个推送的controller

package com.qiushi.wxpush;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;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.WxMpTemplateMessage;/ Created by qcl on 2019-03-28 微信:2501902696 desc: 模版推送仿照/@RestControllerpublic class PushController {/ 微信测试账号推送 /@GetMapping(\公众/push\"大众)public void push() {//1,配置WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();wxStorage.setAppId(\公众wx77bb69292323a000\"大众);wxStorage.setSecret(\公众29bd368145806115ad6820133e62806e\"大众);WxMpService wxMpService = new WxMpServiceImpl();wxMpService.setWxMpConfigStorage(wxStorage);//2,推送WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder().toUser(\"大众o5kho6DgC7SDry8zCmXuvHJGvrgI\"大众)//要推送的用户openid.templateId(\公众Tpln-Eue2obJ0B-8JNkgkiRJaDMPgVeIgGxna982xrg\"大众)//模版id.url(\"大众https://30paotui.com/\"大众)//点击模版要访问的网址.build();//3,如果是正式版发送模版,这里须要配置你的信息// templateMessage.addData(new WxMpTemplateData(\公众name\"大众, \"大众value\"大众, \"大众#FF00FF\"大众));// templateMessage.addData(new WxMpTemplateData(name2, value2, color2));try { wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage); } catch (Exception e) { System.out.println(\"大众推送失落败:\"大众 + e.getMessage()); e.printStackTrace(); }}}

二,接下来就来重点讲讲我们如何注册微信测试账号,并实现推送功能。

正常我们企业开拓,实现微信模版推送,必须要有微信公众年夜众号,备案的网址,并且最麻烦的一点是要获取到用户的openid,作为个人,这些条件基本上都不具备。
以是本日就来带大家注册微信开拓测试账号,来轻松实现微信模版推送。

1,微信扫码登录下面网址https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login扫码登录成功后,就会给我们天生微信公号的appid和appsecret

2,微信扫码关注 测试号二维码,微信给我们返回我们的openid,这个openid在推送时特殊主要。
由于你推送肯定要知道推送给 谁啊,就比如你打电话,肯定要知道用户的电话号码吧。
这个openid便是我们要推送给那个用户的唯一标示。

3,拿到这些往后,我们就可以去实现微信推送了。
推送的代码就只有下面这么点。

//1,配置WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();wxStorage.setAppId(\"大众wx77bb69292323a000\"大众);//appidwxStorage.setSecret(\公众29bd368145806115ad6820133e62806e\"大众);//appsecretWxMpService wxMpService = new WxMpServiceImpl();wxMpService.setWxMpConfigStorage(wxStorage);//2,推送WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder().toUser(\公众o5kho6DgC7SDry8zCmXuvHJGvrgI\公众)//要推送的用户openid.templateId(\公众Tpln-Eue2obJ0B-8JNkgkiRJaDMPgVeIgGxna982xrg\公众)//模版id.url(\"大众https://30paotui.com/\"大众)//点击模版要访问的网址.build();//3,如果是正式版发送模版,这里须要配置你的信息// templateMessage.addData(new WxMpTemplateData(\公众name\"大众, \"大众value\公众, \公众#FF00FF\公众));// templateMessage.addData(new WxMpTemplateData(name2, value2, color2));//发起推送try {String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);System.out.println(\"大众推送成功:\"大众 + msg);} catch (Exception e) {System.out.println(\"大众推送失落败:\公众 + e.getMessage());e.printStackTrace();}

三,推送测试

代码都完成后,我们就可以来测试推送了。
测试我们这个分两种

1,java的单元测试2,运行springboot,通过get要求来触发推送

单元测试

package com.qiushi.wxpush;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import static org.junit.Assert.;/ Created by qcl on 2019-03-28 微信:2501902696 desc:测试用例/@RunWith(SpringRunner.class)@SpringBootTestpublic class PushControllerTest {@AutowiredPushController pushController;@Testpublic void push() {pushController.push();}}

单元测试实在很大略,我们只须要点击箭头所指的绿色按钮,即可运行单元测试,运行通过后就可以看到发送成功了。

java单元测试

log里可以看出我们是10:46发起推送的,看下图我们微信接管到的推送也是10:46

运行springboot,通过get要求来触发推送

这个就更大略了,我们启动springboot项目,然后调用get要求:

image.png

可以看到我们也推送成功了。

到这里我们就轻松通过大略几行代码实现了微信模版推送的功能了。

我们在企业生产环境时,实现这个功能,步骤和这里是一样的。
代码也和这里差不多,只不过多了一个获取用户openid的步骤,这个步骤微信哀求比较严格,必须要有备案的网址作为回调,本日就不给大家深入讲解了,后期我会专门写一篇获取微信用户openid的文章出来。

视频讲解:https://edu.csdn.net/course/detail/23750

编程小石头,为分享干货而生!
听说,每个年轻上进,颜值又高的互联网人都关注我了

标签:

相关文章