微信開發之模板消息


正如許多推送一樣,微信也友好的給廣大開發者提供了“模板消息”,比推送更好的是,它能借助海量用戶的微信平台直接通過服務號以短消息形式傳達給用戶,大大提高了運營的可能性。比如我們現在可以完全拋開銀行卡的短信服務,通過相關銀行提供服務號綁定銀行卡,當發生交易的時候同樣的能收到交易詳情短消息,確實是方便了不少!

 

上一篇講到了獲取和緩存access_token,也成功配置了jssdk授權,這些前置條件都准備好了,那么同樣的實現一些功能就很快了,這回具體來說說模板消息的發送

 

公眾號平台配置

功能-我的模板(或者去模塊庫中搜索),這里不涉及到代碼,不細說

 

 后台restful

實際項目中肯定會存在多種類型的模板,那么肯定需要做一些共用代碼封裝,我這里 以保單出單 這個模板為例

1,對應模板的信息

2.controller

  /**
     * 發送模板消息
     * @return
     */
    @RequestMapping(value = "/sendTemplateMessage", method = RequestMethod.POST) public @ResponseBody HttpResult sendTemplateMessage(@RequestParam String dataJson){ HttpResult hr = null; LOGGER.info("RestFul of sendTemplateMessage parameters dataJson:{}",dataJson); try { hr = wechatService.sendTemplateMessage(dataJson); LOGGER.info("Send template message is successful!",hr); } catch (Exception e) { LOGGER.error("RestFul of sendTemplateMessage is error:{}",e); } return hr; }

因為我這里是一個通用的接口,不同的模板可能傳的參數都不同,時間緣故也沒有寫持久化bean對象,就用了一個json字符串接收

 

3.serveice

直接通過httpclient調用微信提供的POST請求,https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={ACCESS_TOKEN}

  //保單出單通知
    @Value("${TEMPLATE_THREE}")
    private String TEMPLATE_THREE;

 

  /**
     * 發送模板消息
     * @param dataJson
     * @return
     * @throws IOException
     */
    public HttpResult sendTemplateMessage(String dataJson) throws IOException{
        String doUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+getBaseAccessToken(); JSONObject data = JSONObject.parseObject(dataJson); Object touser = data.get("touser");//接收者openid
     String templateId = TEAMLATE_THREE;//模板ID Object url = data.get("url");//模板跳轉鏈接,如果置空,則在發送后,點擊模板消息會進入一個空白頁面(ios),或無法點擊(android)。
Object first = data.getString("first");//標題 Object remark = data.getString("remark");//備注 Object keyword1 = data.getString("keyword1"); Object keyword2 = data.getString("keyword2"); Object keyword3 = data.getString("keyword3"); JSONObject parentJSON = new JSONObject(); parentJSON.put("touser", touser);
     parentJSON.put("template_id", templateId); parentJSON.put("url", url); JSONObject json = new JSONObject(); json.put("first", toJson(first)); json.put("keyword1", toJson(keyword1));//對應的車輛信息 json.put("keyword2", toJson(keyword2));//產品信息 json.put("keyword3", toJson(keyword3));//出單狀態json.put("remark", toJson(remark)); parentJSON.put("data", json);//模板數據 HttpResult rs = null; try { rs = apiService.doPostJson(doUrl,parentJSON.toJSONString()); } catch (Exception e) { LOGGER.error("RestFul of doLogin is error:{}",e); } return rs; } public JSONObject toJson(String value){ JSONObject json = new JSONObject(); json.put("value", value); json.put("color", "#173177");//消息字體顏色 return json; }

為了增強代碼可讀性,關鍵字的地方我都添加了注釋,那么到這里,后台基本完成,下面我通過一個接口調用工具Advanced REST client來測試一下

數據:

{
  "touser": "otjo0wXJZipXdFjxzwDB3DZUjs44", "templateType": "3", "url": "www.liliangel.cn", "first": { "value": "測試發送模板消息3", "color": "#173177" }, "keyword1": { "value": "testCar", "color": "#173177" }, "keyword2": { "value": "testPro", "color": "#173177" }, "keyword3": { "value": "successful", "color": "#173177" }, "remark": { "value": "備注", "color": "#173177" } }

說明:我這里的數據結構是經過了一層json封裝的,詳細的格式可以參考微信官方文檔及模板詳情!

 

成功:

到這里我們已經完成了模板消息的接入,具體會不會比常用的推送更好? 會不會取代手機短信? 看怎么去運營,發揮你的想象,將微信提供的服務最大程度利用!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM