自动采集推送(注册微信开发测试账号实现微信模版消息推送(组图))
优采云 发布时间: 2022-02-01 11:23自动采集推送(注册微信开发测试账号实现微信模版消息推送(组图))
今天就带大家了解一下微信模板消息推送。
先看效果图:
核心代码只有以下几行,可以轻松实现微信模板消息推送
//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后台
至于springboot是如何创建java后端的,这里就不多说了。我们百度一下,很多文章。这里只需要强调以下几点。
com.github.binarywang
weixin-java-mp
3.3.0
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();
}
}
单元测试其实很简单。我们只需要点击箭头所指的绿色按钮即可运行单元测试。操作通过后,我们可以看到消息发送成功。
从日志中可以看出,我们在 10:46 发起了推送。看下图,我们在微信上收到的推送消息也是10点46分。
图像.png
运行springboot通过get请求触发推送
这个就更简单了,我们启动springboot项目,然后调用get请求:
图像.png
可以看到我们也推送成功了。
至此,我们通过几行简单的代码就可以轻松实现微信模板消息推送功能。
当我们在企业生产环境中实现这个功能时,步骤和这里一样。代码和这里类似,但是获取用户的openid多了一步。这一步对微信有更严格的要求,注册的URL必须作为回调。今天就不给大家详细解释了。微信用户openid的文章出来了。