公众号消息推送
今天去编写测试公众号的消息推送
消息推送需要到的jar包:weixin-java-common-3.1.0.jar和weixin-java-mp-3.2.0.jar
消息推送需要到的值:openid、appid、appsecret、AccessToken(https://mp.weixin.qq.com/debug/cgi-bin/apiinfo?t=index)、模板ID
public static void push() { //1,配置 WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage(); wxStorage.setAppId("wx7f1ceeace0d20918"); wxStorage.setSecret("92f6aea5a9d4e3a93023ce1f2cb83e84"); wxStorage.setAccessToken("32__PGUZdML3AtZoHzgFPUd1Y-QwG69yeFWXwM2fB5wBlRWWM8WfgK7D56MEXkxLZ3_HLj57-dGUSmc1sIYm-Mi2TeGc51Gq9j10KmHYmv8DU9dVHzymxkt9s5FQZOg0V5oJsdA8x90F9H7l_YAUZOjAJAYDS");
WxMpService wxMpService = new WxMpServiceImpl(); wxMpService.setWxMpConfigStorage(wxStorage);//配置加载到WxMpServiceImpl中 WxMpTemplateMessage templateMessage=WxMpTemplateMessage.builder()
.toUser("oAGE3s-E3zvpad_p9F45vpFb4v_U")//openid
.templateId("oi-mDbkiTZ7fH4MnhFADYQ6hhhb-sdBVndAzieNWpsE")//模板id
.url("http://baidu.com").build();//跳转地址
List<WxMpTemplateData> templateData; templateMessage.addData(new WxMpTemplateData("first", "您好", "#FF00FF"))//模板数据添加 .addData(new WxMpTemplateData("keyword1", "这是个测试", "#A9A9A9")) .addData(new WxMpTemplateData("keyword2", "这又是个测试", "#FF00FF")) .addData(new WxMpTemplateData("keyword3", "这是个测试", "#A9A9A9")) .addData(new WxMpTemplateData("keyword4", "这又是个测试", "#FF00FF")) .addData(new WxMpTemplateData("remark", "这还是个测试", "#000000")); try { // templateMessage.setToUser("oAGE3s-E3zvpad_p9F45vpFb4v_U");//要推送的用户openid wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage); } catch (Exception e) { System.out.println("推送失败:" + e.getMessage()); e.printStackTrace(); } }
accessToken需要到https://mp.weixin.qq.com/debug/cgi-bin/apiinfo?t=index去配置获取