微信小程序模板消息推送
生活随笔
收集整理的這篇文章主要介紹了
微信小程序模板消息推送
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
時序圖
???????
?
首先,我們需要知道一件事情,小程序的模板推送分為“一次性訂閱”和“長期訂閱”
一次性訂閱:用戶訂閱小程序后,程序只能對指定OpenId進行一次推送模板消息,無法多次推送
長期訂閱:用戶長期訂閱,能夠多次推送模板消息(長期訂閱模板需要向微信官方發起申請)
推送模板消息前置配置
1.需要配置消息推送
2.需要根據AppId、AppSecret調用開發文檔Api獲取access_token?
GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET2.需要使用access_token調用以下Api獲取到微信用戶的open_id
POST https://api.weixin.qq.com/wxa/getpluginopenpid?access_token=ACCESS_TOKEN推送模板消息API
微信開發文檔中的模板消息已經廢棄不進行使用了,我們現在應該使用“訂閱消息”——>“send”這個API(下方鏈接)
subscribeMessage.send | 微信開放文檔
?其中參數的data屬性,需要我們在代碼中與之對應:
例如:
模板格式
參數類型:
?類型:(需要注意一些長度限制)
?上邊的準備好就可以進行代碼編寫。
代碼實現
1.模板消息:(所要傳的參數實體)
public class WxSubscribeMsg {// 接收人idprivate String touser;// 模板idprivate String template_id;// 跳轉小程序private String page;// 模板數據private Map<String, WxTemplateValue> data;// 跳轉小程序類型 默認正式版private String miniprogram_state;// 語言類型 默認中文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.模板內容:(API中的data屬性)
public class WxTemplateValue {private String value;public String getValue() {return value;}public void setValue(String value) {this.value = value;}@Overridepublic String toString() {return "WxTemplateValue{" +"value='" + value + '\'' +'}';} }?3.返回數據接收
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;} }?推送代碼:
調用的API接口:
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);// 模板消息到小程序跳轉頁面wxSubscribeMsg.setPage("pages/workbench/workbench?id=" + dto.getId()+"&pageTpye=task"); // 開發版 // wxSubscribeMsg.setMiniprogram_state("developer"); // 跳轉體驗版wxSubscribeMsg.setMiniprogram_state("trial"); // 跳轉正式版 // wxSubscribeMsg.setMiniprogram_state("formal");// 模板消息Map<String, WxTemplateValue> map = new HashMap<>();// 發布內容WxTemplateValue keyword1 = new WxTemplateValue();keyword1.setValue(sendContent);map.put("thing1", keyword1);// 影響區域WxTemplateValue keyword2 = new WxTemplateValue();String taskArea = dto.getSecondAreaName()+dto.getThirdAreaName();if(taskArea.length() < 20){keyword2.setValue(taskArea);}else{keyword2.setValue("");}map.put("thing2", keyword2);// 發布時間WxTemplateValue keyword3 = new WxTemplateValue();String date = dto.getTaskStartTime()keyword3.setValue(date);map.put("date3", keyword3);// 發布單位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);// 推送模板參數wxSubscribeMsg.setData(map);// 參數轉jsonString json = JSONObject.toJSONString(wxSubscribeMsg);// 調用微信推送模板接口String doPostJson = HttpClientUtil.doPostJson(requestUrl, json);// 將獲取到的數據進行判斷進行日志寫入JSONObject jsonObject = JSONObject.parseObject(doPostJson);LOGGER.info("調用微信模板消息回調結果:"+ com.wanwei.oneview.base.utils.JsonUtils.objectToJson(jsonObject));總結
以上是生活随笔為你收集整理的微信小程序模板消息推送的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cJONS序列化工具解读二(数据解析)
- 下一篇: 立体标定