時(shí)序圖
???????
?
首先,我們需要知道一件事情,小程序的模板推送分為“一次性訂閱”和“長(zhǎng)期訂閱”
一次性訂閱:用戶(hù)訂閱小程序后,程序只能對(duì)指定OpenId進(jìn)行一次推送模板消息,無(wú)法多次推送
長(zhǎng)期訂閱:用戶(hù)長(zhǎng)期訂閱,能夠多次推送模板消息(長(zhǎng)期訂閱模板需要向微信官方發(fā)起申請(qǐng))
推送模板消息前置配置
1.需要配置消息推送
2.需要根據(jù)AppId、AppSecret調(diào)用開(kāi)發(fā)文檔Api獲取access_token?
GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
2.需要使用access_token調(diào)用以下Api獲取到微信用戶(hù)的open_id
POST https://api.weixin.qq.com/wxa/getpluginopenpid?access_token=ACCESS_TOKEN
推送模板消息API
微信開(kāi)發(fā)文檔中的模板消息已經(jīng)廢棄不進(jìn)行使用了,我們現(xiàn)在應(yīng)該使用“訂閱消息”——>“send”這個(gè)API(下方鏈接)
subscribeMessage.send | 微信開(kāi)放文檔
?其中參數(shù)的data屬性,需要我們?cè)诖a中與之對(duì)應(yīng):
例如:
模板格式
參數(shù)類(lèi)型:
?類(lèi)型:(需要注意一些長(zhǎng)度限制)
?上邊的準(zhǔn)備好就可以進(jìn)行代碼編寫(xiě)。
代碼實(shí)現(xiàn)
1.模板消息:(所要傳的參數(shù)實(shí)體)
public class WxSubscribeMsg { // 接收人id private String touser; // 模板id private String template_id; // 跳轉(zhuǎn)小程序 private String page; // 模板數(shù)據(jù) private Map<String, WxTemplateValue> data; // 跳轉(zhuǎn)小程序類(lèi)型 默認(rèn)正式版 private String miniprogram_state; // 語(yǔ)言類(lèi)型 默認(rèn)中文 private String lang = "zh_CN"; 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 Map<String, WxTemplateValue> getData() { return data; } public void setData(Map<String, WxTemplateValue> data) { this.data = data; } public String getMiniprogram_state() { return miniprogram_state; } public void setMiniprogram_state(String miniprogram_state) { this.miniprogram_state = miniprogram_state; } public String getLang() { return lang; } public void setLang(String lang) { this.lang = lang; } }
?2.模板內(nèi)容:(API中的data屬性)
public class WxTemplateValue { private String value; public String getValue() { return value; } public void setValue(String value) { this.value = value; } @Override public String toString() { return "WxTemplateValue{" + "value='" + value + '\'' + '}'; } }
?3.返回?cái)?shù)據(jù)接收
public class WxUserInfo { private String openid; private String session_key; private String unionid; private int errcode; private String errmsg; public String getOpenid() { return openid; } public void setOpenid(String openid) { this.openid = openid; } public String getSession_key() { return session_key; } public void setSession_key(String session_key) { this.session_key = session_key; } public String getUnionid() { return unionid; } public void setUnionid(String unionid) { this.unionid = unionid; } public int getErrcode() { return errcode; } public void setErrcode(int errcode) { this.errcode = errcode; } public String getErrmsg() { return errmsg; } public void setErrmsg(String errmsg) { this.errmsg = errmsg; } }
?推送代碼:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-500868.html
調(diào)用的API接口:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-500868.html
POST https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN
WxSubscribeMsg wxSubscribeMsg = new WxSubscribeMsg(); wxSubscribeMsg.setTouser(openId); wxSubscribeMsg.setTemplate_id(templateId); // 模板消息到小程序跳轉(zhuǎn)頁(yè)面 wxSubscribeMsg.setPage("pages/workbench/workbench?id=" + dto.getId()+"&pageTpye=task"); // 開(kāi)發(fā)版 // wxSubscribeMsg.setMiniprogram_state("developer"); // 跳轉(zhuǎn)體驗(yàn)版 wxSubscribeMsg.setMiniprogram_state("trial"); // 跳轉(zhuǎn)正式版 // wxSubscribeMsg.setMiniprogram_state("formal"); // 模板消息 Map<String, WxTemplateValue> map = new HashMap<>(); // 發(fā)布內(nèi)容 WxTemplateValue keyword1 = new WxTemplateValue(); keyword1.setValue(sendContent); map.put("thing1", keyword1); // 影響區(qū)域 WxTemplateValue keyword2 = new WxTemplateValue(); String taskArea = dto.getSecondAreaName()+dto.getThirdAreaName(); if(taskArea.length() < 20){ keyword2.setValue(taskArea); }else{ keyword2.setValue(""); } map.put("thing2", keyword2); // 發(fā)布時(shí)間 WxTemplateValue keyword3 = new WxTemplateValue(); String date = dto.getTaskStartTime() keyword3.setValue(date); map.put("date3", keyword3); // 發(fā)布單位 WxTemplateValue keyword4 = new WxTemplateValue(); String group = dto.getName(); if(group.length() < 20){ keyword4.setValue(group); }else{ String substring = group.substring(0, 20); keyword4.setValue(substring); } map.put("thing4", keyword4); // 推送模板參數(shù) wxSubscribeMsg.setData(map); // 參數(shù)轉(zhuǎn)json String json = JSONObject.toJSONString(wxSubscribeMsg); // 調(diào)用微信推送模板接口 String doPostJson = HttpClientUtil.doPostJson(requestUrl, json); // 將獲取到的數(shù)據(jù)進(jìn)行判斷進(jìn)行日志寫(xiě)入 JSONObject jsonObject = JSONObject.parseObject(doPostJson); LOGGER.info("調(diào)用微信模板消息回調(diào)結(jié)果:"+ com.wanwei.oneview.base.utils.JsonUtils.objectToJson(jsonObject));
到了這里,關(guān)于微信小程序模板消息推送的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!