微信公众号模板消息开发:如何实现模板消息的二次开发?

2026-05-27 21:541阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计834个文字,预计阅读时间需要4分钟。

微信公众号模板消息开发:如何实现模板消息的二次开发?

前言:根据公众平台提供的API,需要注意以下几点:

1.access_token需要缓存

2.需要按所选模板封装应用的数据

3.推送消息必须满足条件:模板id、被推送者的openid

模板消息接口

编辑


前言:

根据公众平台提供api,需要注意以下几点:

①,access_token需要缓存

②,需要按照所选模板封装对应的数据

③,推送消息必须条件:模板id,被推送者的openid

​​模板消息接口​​

微信公众号模板消息开发:如何实现模板消息的二次开发?

​编辑

开发实现:

下面以实现绑定推送消息为例:

​编辑

①:获取access_token(由于每天access_token获取有次数限制,需要缓存)

/**
* 获取access_token
* @param AppId
* @param AppSecret
* @return
* @throws
public static String getAccessToken(String AppId,String AppSecret)throws BusinessException{
String access_tokenurl="api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
access_tokenurl=access_tokenurl.replace("APPID",AppId);
access_tokenurl=access_tokenurl.replace("APPSECRET",AppSecret);
String accesstoken="";
JSONObject jsonObject = WeiXinUtil.api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN

/**
* 公众号推送封装
* @param vo
* @param ACCESS_TOKEN
* @throws
public static String sendMessagePush(GZHMsgVo vo,String ACCESS_TOKEN){
JSONObject json=new JSONObject();
JSONObject text=new JSONObject();
JSONObject first=new JSONObject();
JSONObject remark=new JSONObject();
if (vo.getTouser()==null){
return "touser不能为空";
}
if (vo.getTemplateId()==null){
return "TemplateId不能为空";
}
json.put("touser",vo.getTouser());
json.put("template_id",vo.getTemplateId());
first.put("value",vo.getTitle());
remark.put("value",vo.getRemark());
if (vo.getKeyword1()!=null){
JSONObject keyword1=new JSONObject();
keyword1.put("value",vo.getKeyword1());
text.put("keyword1",keyword1);
}
if (vo.getKeyword2()!=null){
JSONObject keyword2=new JSONObject();
keyword2.put("value",vo.getKeyword2());
text.put("keyword2",keyword2);
}
if (vo.getKeyword3()!=null){
JSONObject keyword3=new JSONObject();
keyword3.put("value",vo.getKeyword3());
text.put("keyword3",keyword3);
}
if (vo.getKeyword4()!=null){
JSONObject keyword4=new JSONObject();
keyword4.put("value",vo.getKeyword4());
text.put("keyword4",keyword4);
}
if (vo.getKeyword5()!=null){
JSONObject keyword5=new JSONObject();
keyword5.put("value",vo.getKeyword5());
text.put("keyword5",keyword5);
}
if (vo.getTitleColor() !=null){
first.put("color",vo.getTitleColor());
}
if (vo.getRemakColor() !=null){
remark.put("color",vo.getRemakColor());
}
text.put("first", first);
text.put("remark",remark);
json.put("data", text);
//发送模板消息
String sendUrl="api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
sendUrl=sendUrl.replace("ACCESS_TOKEN",ACCESS_TOKEN);
String jsonstringParm =json.toString();
JSONObject object=WXAdvancedUtil.httpsRequest(sendUrl,"POST",jsonstringParm);
return

⑤,WXAdvancedUtil.httpsRequest(sendUrl,"POST",jsonstringParm)

/**
* 发送http请求
* @param requestUrl
* @param requestMethod
* @param outputStr
* @return
public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) {
JSONObject jsonObject = null;
try {
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();

URL url = new URL(requestUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setSSLSocketFactory(ssf);

conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// 设置请求方式(GET/POST)
conn.setRequestMethod(requestMethod);

// 当outputStr不为null时向输出流写数据
if (null != outputStr) {
OutputStream outputStream = conn.getOutputStream();
// 注意编码格式
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}

// 从输入流读取返回内容
InputStream inputStream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}

// 释放资源
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
conn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
} catch (ConnectException ce) {

ce.printStackTrace();
} catch (Exception e) {

e.printStackTrace();
}
return

效果:

编辑

本文共计834个文字,预计阅读时间需要4分钟。

微信公众号模板消息开发:如何实现模板消息的二次开发?

前言:根据公众平台提供的API,需要注意以下几点:

1.access_token需要缓存

2.需要按所选模板封装应用的数据

3.推送消息必须满足条件:模板id、被推送者的openid

模板消息接口

编辑


前言:

根据公众平台提供api,需要注意以下几点:

①,access_token需要缓存

②,需要按照所选模板封装对应的数据

③,推送消息必须条件:模板id,被推送者的openid

​​模板消息接口​​

微信公众号模板消息开发:如何实现模板消息的二次开发?

​编辑

开发实现:

下面以实现绑定推送消息为例:

​编辑

①:获取access_token(由于每天access_token获取有次数限制,需要缓存)

/**
* 获取access_token
* @param AppId
* @param AppSecret
* @return
* @throws
public static String getAccessToken(String AppId,String AppSecret)throws BusinessException{
String access_tokenurl="api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
access_tokenurl=access_tokenurl.replace("APPID",AppId);
access_tokenurl=access_tokenurl.replace("APPSECRET",AppSecret);
String accesstoken="";
JSONObject jsonObject = WeiXinUtil.api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN

/**
* 公众号推送封装
* @param vo
* @param ACCESS_TOKEN
* @throws
public static String sendMessagePush(GZHMsgVo vo,String ACCESS_TOKEN){
JSONObject json=new JSONObject();
JSONObject text=new JSONObject();
JSONObject first=new JSONObject();
JSONObject remark=new JSONObject();
if (vo.getTouser()==null){
return "touser不能为空";
}
if (vo.getTemplateId()==null){
return "TemplateId不能为空";
}
json.put("touser",vo.getTouser());
json.put("template_id",vo.getTemplateId());
first.put("value",vo.getTitle());
remark.put("value",vo.getRemark());
if (vo.getKeyword1()!=null){
JSONObject keyword1=new JSONObject();
keyword1.put("value",vo.getKeyword1());
text.put("keyword1",keyword1);
}
if (vo.getKeyword2()!=null){
JSONObject keyword2=new JSONObject();
keyword2.put("value",vo.getKeyword2());
text.put("keyword2",keyword2);
}
if (vo.getKeyword3()!=null){
JSONObject keyword3=new JSONObject();
keyword3.put("value",vo.getKeyword3());
text.put("keyword3",keyword3);
}
if (vo.getKeyword4()!=null){
JSONObject keyword4=new JSONObject();
keyword4.put("value",vo.getKeyword4());
text.put("keyword4",keyword4);
}
if (vo.getKeyword5()!=null){
JSONObject keyword5=new JSONObject();
keyword5.put("value",vo.getKeyword5());
text.put("keyword5",keyword5);
}
if (vo.getTitleColor() !=null){
first.put("color",vo.getTitleColor());
}
if (vo.getRemakColor() !=null){
remark.put("color",vo.getRemakColor());
}
text.put("first", first);
text.put("remark",remark);
json.put("data", text);
//发送模板消息
String sendUrl="api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
sendUrl=sendUrl.replace("ACCESS_TOKEN",ACCESS_TOKEN);
String jsonstringParm =json.toString();
JSONObject object=WXAdvancedUtil.httpsRequest(sendUrl,"POST",jsonstringParm);
return

⑤,WXAdvancedUtil.httpsRequest(sendUrl,"POST",jsonstringParm)

/**
* 发送http请求
* @param requestUrl
* @param requestMethod
* @param outputStr
* @return
public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) {
JSONObject jsonObject = null;
try {
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();

URL url = new URL(requestUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setSSLSocketFactory(ssf);

conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// 设置请求方式(GET/POST)
conn.setRequestMethod(requestMethod);

// 当outputStr不为null时向输出流写数据
if (null != outputStr) {
OutputStream outputStream = conn.getOutputStream();
// 注意编码格式
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}

// 从输入流读取返回内容
InputStream inputStream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}

// 释放资源
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
conn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
} catch (ConnectException ce) {

ce.printStackTrace();
} catch (Exception e) {

e.printStackTrace();
}
return

效果:

编辑