准备工作
- 申请微信公众号,并进行认证(未认证的公众号没有推送消息的权限)
- 将需要进行推送环境的IP地址加入到白名单
下面是实现代码
package com.xiaofeixiang;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
@SpringBootTest
public class SendMsg {
private String appid = "";// 设置自己公众号的
private String appsecret = "";// 设置自己公众号的
@Test
public void Test() throws IOException, JSONException {
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
// String formattedDate = sdf.format(new Date());
String ret = getToken();
System.out.println(ret);
JsonObject retObj = JsonParser.parseString(ret).getAsJsonObject();
String token = String.valueOf(retObj.get("access_token")).replace("\"", "");
//"o-I2W6qLGzs0Fg7wnU9qAtfsP-SA","o-I2W6gcFUCM0gFP5hm4aIi4s9qw","o-I2W6oDLbFgtpkaqVpUXiBw5fVg"
// List<String> openidList = List.of("o-I2W6qLGzs0Fg7wnU9qAtfsP-SA", "o-I2W6gcFUCM0gFP5hm4aIi4s9qw", "o-I2W6oDLbFgtpkaqVpUXiBw5fVg");
// for (String openid : openidList) {
// postMsg(token, openid);
// }
postMsg(token, "o-I2W6qLGzs0Fg7wnU9qAtfsP-SA");
}
/**
* 获取token
* @return
* @throws IOException
*/
public String getToken() throws IOException {
// 创建httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+appsecret;
// 创建请求对象
HttpGet httpGet = new HttpGet(url);
// 发送请求,接受响应结果
CloseableHttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
String body = EntityUtils.toString(entity);
response.close();
httpClient.close();
return body;
}
/**
* 发送模板消息
* @param token
* @throws JSONException
* @throws IOException
*/
public void postMsg(String token, String openid) throws JSONException, IOException {
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+token;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
JSONObject jsonObject = new JSONObject();
jsonObject.put("touser", openid);
jsonObject.put("template_id", "zYyux3vDiHlX0JvfLyK1CdDDV9X4OFxWZV5A3fITQyM");
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("thing3", new JSONObject().put("value", new String("订单名称".getBytes("utf-8"), "ISO-8859-1")));
jsonObject1.put("character_string13", new JSONObject().put("value", "aaaaaaaaaa"));
jsonObject1.put("time5", new JSONObject().put("value", "2024-07-29"));
jsonObject.put("data", jsonObject1);
System.out.println(jsonObject.toString());
StringEntity entity = new StringEntity(jsonObject.toString());
// entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity entity2 = response.getEntity();
String body = EntityUtils.toString(entity2);
System.out.println(body);
response.close();
httpClient.close();
}
public JsonObject getAllOpenid(String token) throws IOException {
// 创建httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
String url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token="+token;
// 创建请求对象
HttpGet httpGet = new HttpGet(url);
// 发送请求,接受响应结果
CloseableHttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
String body = EntityUtils.toString(entity);
response.close();
httpClient.close();
return JsonParser.parseString(body).getAsJsonObject();
}
}
公众号收到推送消息如下