您好,登錄后才能下訂單哦!
本文實(shí)例為大家分享了java微信公眾號(hào)發(fā)送消息模板的具體代碼,供大家參考,具體內(nèi)容如下
這段時(shí)間接觸公眾號(hào)開發(fā),寫下向用戶發(fā)送消息模板的接口調(diào)用
先上接口代碼
public static JSONObject sendModelMessage(ServletContext context,JSONObject jsonMsg) { System.out.println("消息內(nèi)容:"+jsonMsg); boolean result = false; try { getWX_AccessToken(context); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } // 拼接請(qǐng)求地址 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()); // 發(fā)送客服消息 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("模板消息發(fā)送成功 errcode:{} "+errorCode+"----"+errorMsg); } else { System.out.println("模板消息發(fā)送失敗 errcode:{} "+errorCode+"----"+errorMsg); } } return null; }
15行那段getJsonByWX是統(tǒng)一調(diào)用微信接口的方法,每個(gè)項(xiàng)目都有自己的調(diào)用方法,我這里就不貼了。接口調(diào)用鏈接:https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN
接下來就是建個(gè)bean類,里面寫入一下顏色及值
private String value; private String color; public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String getColor() { return color; } public void setColor(String color) { this.color = color; }
在公眾號(hào)里填寫模板消息的對(duì)應(yīng)格式
之后就是有個(gè)觸發(fā)點(diǎn),我選擇發(fā)貨后把發(fā)貨信息發(fā)送給用戶
PageData wechatTemplate = new PageData(); wechatTemplate.put("template_id", "填寫你的模板id"); wechatTemplate.put("touser", userInfo.get("openid"));//獲取用戶的openid Map<String,TemplateMessageUtil> mapdata = new HashMap<>(); TemplateMessageUtil first = new TemplateMessageUtil(); first.setColor("#173177"); first.setValue("發(fā)貨通知"); mapdata.put("first", first); TemplateMessageUtil text1 = new TemplateMessageUtil(); text1.setColor("#173177"); text1.setValue("您好,您所購買的商品已發(fā)貨。"); mapdata.put("text1", text1); TemplateMessageUtil text2 = new TemplateMessageUtil(); text2.setColor("#173177"); text2.setValue(expresser_name); mapdata.put("text2", text2); TemplateMessageUtil text3 = new TemplateMessageUtil(); text3.setColor("#173177"); text3.setValue(expresser_phone); mapdata.put("text3", text3); TemplateMessageUtil remark = new TemplateMessageUtil(); remark.setColor("#173177"); remark.setValue("請(qǐng)保持電話暢通>>"); mapdata.put("remark", remark); JSONObject json = new JSONObject(); json.put("data",mapdata); json.putAll(wechatTemplate);//轉(zhuǎn)為json WXInterface.sendModelMessage(context,json);
之后手機(jī)就會(huì)收到信息了
整體思路是這樣,也是參照百度而來,因?yàn)槊總€(gè)人的項(xiàng)目里方法都不一樣,我就不詳細(xì)貼上,既然做到發(fā)送模板消息了,統(tǒng)一調(diào)用微信接口的方法應(yīng)每個(gè)人該也早寫在工具類里了,每個(gè)人都不同,當(dāng)應(yīng)該都有,調(diào)用這個(gè)方法,把微信模板消息連接的條件access_token寫進(jìn)去就請(qǐng)求了,剩下的就是傳入你要發(fā)送的消息,消息存入集合,集合轉(zhuǎn)json才行,JSONObject類相信也都有,我也不貼了,每個(gè)人項(xiàng)目都不一樣,沒必要照搬過去,就照著自己原先已有的類改進(jìn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。