小程序订阅消息推送
前端添加触发函数
subscribeTest: function(){
wx.requestSubscribeMessage({
tmplIds: [''],
success (res) { },
fail(err){
console.log('err',err)
}
})
},
后端发送模板消息函数
private static final String SUBSCRIBE_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send";
public static JsonObject sendSubscribeMessage(String accessToken, String userOpenId, String templateId, String page, String miniprogramState, String lang, JsonObject data) {
String sendTemplateMessageUrl = String.format("%s?access_token=%s", SUBSCRIBE_SEND_URL, accessToken);
JsonObject requestBody = new JsonObject();
requestBody.addProperty("touser", userOpenId);
requestBody.addProperty("template_id", templateId);
requestBody.addProperty("page", page);
requestBody.addProperty("miniprogram_state", miniprogramState);
requestBody.addProperty("lang", lang);
requestBody.add("data", data);
try {
return checkWeChatResponse(HttpUtil.post(sendTemplateMessageUrl, requestBody.toString()));
} catch (Exception e) {
log.error("订阅消息发送消息异常", e);
}
}
```