订阅消息是微信近期新出的一个能力,用来代替原有的模板消息(原有的模板消息即将下线)
订阅消息的功能较模板消息有所提升,"7天"的限制取消,同时有"一次性"和"永久"订阅.(功能上是这样说的,但是实际开发时发现"永久"订阅还是对小程序的服务类目有要求的,客户的小程序只支持"一次性"订阅)
我是因为用了发模板消息的方法发送订阅消息,我以前发送模板消息Key我是自定义的,不需要和模板上的一致,导致发用订阅消息的时候Key对应不上就会出现如下图的异常:
public class TemplateParam {
private String key;
private String value;
public TemplateParam(String key, String value) {
this.key = key;
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}
public class Template {
private String touser;
private String template_id;
private String page;
private List templateParamList;
public String getTouser() {
return touser;
}
public void setTouser(String touser) {
this.touser = touser;
}
public String getTemplate_id() {
return template_id;
}
public void setTemplate_id(String template_id) {
this.template_id = template_id;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public String toJSON() {
StringBuffer buffer = new StringBuffer();
buffer.append("{");
buffer.append(String.format("\"touser\":\"%s\"", this.touser)).append(",");
buffer.append(String.format("\"template_id\":\"%s\"", this.template_id)).append(",");
buffer.append(String.format("\"page\":\"%s\"", this.page)).append(",");
buffer.append("\"data\":{");
TemplateParam param = null;
for (int i = 0; i < this.templateParamList.size(); i++) {
param = templateParamList.get(i);
// 判断是否追加逗号
if (i < this.templateParamList.size() - 1) {
buffer.append(String.format("\"%s\": {\"value\":\"%s\"},", param.getKey(), param.getValue()));
} else {
buffer.append(String.format("\"%s\": {\"value\":\"%s\"}", param.getKey(), param.getValue()));
}
}
buffer.append("}");
buffer.append("}");
return buffer.toString();
}
public List getTemplateParamList() {
return templateParamList;
}
public void setTemplateParamList(List templateParamList) {
this.templateParamList = templateParamList;
}
}
Template template=new Template();
template.setTemplate_id("模板ID");
template.setTouser(("OPENID"));
template.setPage("pages/index/index");
List paras=new ArrayList();
paras.add(new TemplateParam("thing2","店铺审核"));
paras.add(new TemplateParam("phrase1",msg));
paras.add(new TemplateParam("date3",DateUtil.getTime()));
template.setTemplateParamList(paras);
System.err.println(template.toJSON());
String result = ModeUtil.wxMessageModeSendUrl(wxPayConfig.getAppid(), wxPayConfig.getAppsecret(), template);
public static String wxMessageModeSendUrl(String appid,String secret,Template template) {
String tmpurl = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN";
String token = ModeUtil.getToken(appid, secret);
System.err.println(token);
String url = tmpurl.replace("ACCESS_TOKEN", token);
System.err.println(url);
String result = HttpRequest.sendPost(url, template.toJSON());
System.err.println(result);
try {
JSONObject resultJson = JSONObject.fromObject(result);
String errmsg = (String) resultJson.get("errmsg");
System.err.println(errmsg);
if ("ok".equals(errmsg)) {
return "success";
}
return "error";
} catch (JSONException e) {
e.printStackTrace();
}
return "success";
}