java微信公众号推送消息推送消息_使用Java实现微信公众号推送模板消息(保证能用)...

背景:

还是那个业务背景,面向微信公众号开发,指定针对用户发送消息,胖友们,我研究了一天了,代码粘过去,用不了你锤死我

1、pom.xml文件中添加依赖

org.projectlombok

lombok

org.wso2.apache.httpcomponents

httpmime

4.3.1.wso2v1

org.wso2.apache.httpcomponents

httpclient

4.3.1.wso2v1

top.jfunc.json

json-fastjson

1.8.3

DataEntity.java

@Getter

@Setterpublic classDataEntity {//内容

privateString value;//字体颜色

privateString color;publicDataEntity(String value ,String color){this.value =value;this.color =color;

}

实现类: 有“XXX”的地方需要修改,其他地方可以不用动

WechatMsg.java

@Componentpublic classWechatMsg {public static voidmain(String[] args) {

WechatMsg wm= newWechatMsg();

wm.SendWeChatMsg(wm.getToken());

}/*** 获取token

*

*@returntoken*/

publicString getToken() {//授予形式

String grant_type = "client_credential";//应用ID

String appid = "XXXXXXXXXXXXXXXXXXX";//密钥

String secret = "XXXXXXXXXXXXXXXXXXXXXXXXXX";//接口地址拼接参数

String getTokenApi = "https://api.weixin.qq.com/cgi-bin/token?grant_type=" + grant_type + "&appid=" +appid+ "&secret=" +secret;

String tokenJsonStr= doGetPost(getTokenApi, "GET", null);

JSONObject tokenJson=JSONObject.parseObject(tokenJsonStr);

String token= tokenJson.get("access_token").toString();

System.out.println("获取到的TOKEN : " +token);returntoken;

}/***

* 发送消息

*

*@paramtoken*/

public voidSendWeChatMsg(String token) {//接口地址

String sendMsgApi = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+token;//openId

String toUser = "XXXXXXXXXXXXXX";//消息模板ID

String template_id = "XXXXXXXXXXXXXXXXXXXXXX";//整体参数map

Map paramMap = new HashMap();//消息主题显示相关map

Map dataMap = new HashMap();//根据自己的模板定义内容和颜色

dataMap.put("first",new DataEntity("详细内容XXXXXXX","#173177"));

dataMap.put("keyword1",new DataEntity("私有化部署XXX","#173177"));

dataMap.put("keyword2",new DataEntity("2020-08-18XXX" ,"#173177"));

dataMap.put("remark",new DataEntity("申请成功XXX","#173177"));

paramMap.put("touser", toUser);

paramMap.put("template_id", template_id);

paramMap.put("data", dataMap);

System.out.println(doGetPost(sendMsgApi,"POST",paramMap));

}/*** 调用接口 post

*@paramapiPath*/

public String doGetPost(String apiPath,String type,MapparamMap){

OutputStreamWriter out= null;

InputStream is= null;

String result= null;try{

URL url= new URL(apiPath);//创建连接

HttpURLConnection connection =(HttpURLConnection) url.openConnection();

connection.setDoOutput(true);

connection.setDoInput(true);

connection.setUseCaches(false);

connection.setInstanceFollowRedirects(true);

connection.setRequestMethod(type) ;//设置请求方式

connection.setRequestProperty("Accept", "application/json"); //设置接收数据的格式

connection.setRequestProperty("Content-Type", "application/json"); //设置发送数据的格式

connection.connect();if(type.equals("POST")){

out= new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); //utf-8编码

out.append(JSON.toJSONString(paramMap));

out.flush();

out.close();

}//读取响应

is =connection.getInputStream();int length = (int) connection.getContentLength();//获取长度

if (length != -1) {byte[] data = new byte[length];byte[] temp = new byte[512];int readLen = 0;int destPos = 0;while ((readLen = is.read(temp)) > 0) {

System.arraycopy(temp,0, data, destPos, readLen);

destPos+=readLen;

}

result= new String(data, "UTF-8"); //utf-8编码

}

}catch(IOException e) {

e.printStackTrace();

}finally{try{

is.close();

}catch(IOException e) {

e.printStackTrace();

}

}returnresult;

}

}

效果图

0a0d4c4a5a7bcb1ac4c0919ad3e61816.png

附微信接口文档:https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值