
视频讲解:
先看效果图:

核心代码只有以下几行微信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());
}
至于springboot怎么创建java后台,我这儿就不再唠叨了,大家百度一下,一大堆的文章。这里只应该重点讲解下下面几点。
<!--微信模版消息推送三方sdk-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>3.3.0</version>
</dependency>
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: 模版消息推送模拟
*/
@RestController
public 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,配置
WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
wxStorage.setAppId("wx77bb69292323a000");//appid
wxStorage.setSecret("29bd368145806115ad6820133e62806e");//appsecret
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 {
String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
System.out.println("推送成功:" + msg);
} catch (Exception e) {
System.out.println("推送失败:" + e.getMessage());
e.printStackTrace();
}
代码都完成后,我们就可以来测试推送了。测试我们这个分两种
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)
@SpringBootTest
public class PushControllerTest {
@Autowired
PushController pushController;
@Test
public void push() {
pushController.push();
}
}
单元测试其实很简单,我们只应该点击箭头所指的蓝标,即可运行单元测试,运行通过后就可以看见发送消息失败了。


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

这个就更简单了,我们开启springboot项目,然后调用get请求:



可以发现我们也推送成功了。
到此处我们就轻松借助简单几行代码实现了微信模版消息推送的功能了。
我们在企业制造环境时,实现这个用途,步骤和此处是一样的。代码也和这儿差不多微信java,只不过多了一个获取客户openid的流程,这个方法微信要求非常苛刻,必须要有备案的网址作为回调,今天就不给你们深入讲解了,后期我会专门写一篇获取微信用户openid的文章出来。
如果你有微信或者java开发方面的难题,可以加我微信交流学习:2501902696。也可以加我微信获取完整源码。
视频讲解:
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/sanxing/article-125277-1.html
很对
巴菲特
老马啊巴菲特他真的是靠股票发家的