十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
本文实例为大家分享了java微信公众号发送消息模板的具体代码,供大家参考,具体内容如下

这段时间接触公众号开发,写下向用户发送消息模板的接口调用
先上接口代码
public static JSONObject sendModelMessage(ServletContext context,JSONObject jsonMsg) {
System.out.println("消息内容:"+jsonMsg);
boolean result = false;
try {
getWX_AccessToken(context);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 拼接请求地址
String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
requestUrl = requestUrl.replace("ACCESS_TOKEN", context.getAttribute(ContextTokenName).toString());
// 发送客服消息
JSONObject jsonObject = getJsonByWX(requestUrl, context, "POST",jsonMsg, false);
if (null != jsonObject) {
int errorCode = jsonObject.getInt("errcode");
String errorMsg = jsonObject.getString("errmsg");
if (0 == errorCode) {
result = true;
System.out.println("模板消息发送成功 errcode:{} "+errorCode+"----"+errorMsg);
} else {
System.out.println("模板消息发送失败 errcode:{} "+errorCode+"----"+errorMsg);
}
}
return null;
}