正如许多推送一样,微信也友好的给广大开发者提供了“模板消息”,比推送更好的是,它能借助海量用户的微信平台直接通过服务号以短消息形式传达给用户,大大提高了运营的可能性。比如我们现在可以完全抛开银行卡的短信服务,通过相关银行提供服务号绑定银行卡,当发生交易的时候同样的能收到交易详情短消息,确实是方便了不少!

 

上一篇讲到了获取和缓存access_token,也成功配置了jssdk授权,这些前置条件都准备好了,那么同样的实现一些功能就很快了,这回具体来说说模板消息的发送

功能-我的模板(或者去模块库中搜索),这里不涉及到代码,不细说

 

实际项目中肯定会存在多种类型的模板,那么肯定需要做一些共用代码封装,我这里 以保单出单 这个模板为例

  1.   /**
  2. * 发送模板消息
  3. * @return
  4. */
  5. @RequestMapping(value = "/sendTemplateMessage", method = RequestMethod.POST)
  6. public @ResponseBody HttpResult sendTemplateMessage(@RequestParam String dataJson){
  7. HttpResult hr = null;
  8. LOGGER.info("RestFul of sendTemplateMessage parameters dataJson:{}",dataJson);
  9. try {
  10. hr = wechatService.sendTemplateMessage(dataJson);
  11. LOGGER.info("Send template message is successful!",hr);
  12. } catch (Exception e) {
  13. LOGGER.error("RestFul of sendTemplateMessage is error:{}",e);
  14. }
  15. return hr;
  16. }

因为我这里是一个通用的接口,不同的模板可能传的参数都不同,时间缘故也没有写持久化bean对象,就用了一个json字符串接收

 

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

  1.   //保单出单通知
  2. @Value("${TEMPLATE_THREE}")
  3. private String TEMPLATE_THREE;

 

  1.   /**
  2. * 发送模板消息
  3. * @param dataJson
  4. * @return
  5. * @throws IOException
  6. */
  7. public HttpResult sendTemplateMessage(String dataJson) throws IOException{
  8. String doUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+getBaseAccessToken();
  9. JSONObject data = JSONObject.parseObject(dataJson);
  10. Object touser = data.get("touser");//接收者openid
         String templateId = TEAMLATE_THREE;//模板ID
  11. Object url = data.get("url");//模板跳转链接,如果置空,则在发送后,点击模板消息会进入一个空白页面(ios),或无法点击(android)。
  12. Object first = data.getString("first");//标题
  13. Object remark = data.getString("remark");//备注
  14. Object keyword1 = data.getString("keyword1");
  15. Object keyword2 = data.getString("keyword2");
  16. Object keyword3 = data.getString("keyword3");
  17. JSONObject parentJSON = new JSONObject();
  18. parentJSON.put("touser", touser);
         parentJSON.put("template_id", templateId);
  19. parentJSON.put("url", url);
  20. JSONObject json = new JSONObject();
  21. json.put("first", toJson(first));
  22. json.put("keyword1", toJson(keyword1));//对应的车辆信息
  23. json.put("keyword2", toJson(keyword2));//产品信息
  24. json.put("keyword3", toJson(keyword3));//出单状态json.put("remark", toJson(remark));
  25. parentJSON.put("data", json);//模板数据
  26. HttpResult rs = null;
  27. try {
  28. rs = apiService.doPostJson(doUrl,parentJSON.toJSONString());
  29. } catch (Exception e) {
  30. LOGGER.error("RestFul of doLogin is error:{}",e);
  31. }
  32. return rs;
  33. }
  34. public JSONObject toJson(String value){
  35. JSONObject json = new JSONObject();
  36. json.put("value", value);
  37. json.put("color", "#173177");//消息字体颜色
  38. return json;
  39. }

为了增强代码可读性,关键字的地方我都添加了注释,那么到这里,后台基本完成,下面我通过一个接口调用工具Advanced REST client来测试一下

数据:

  1. {
  2. "touser": "otjo0wXJZipXdFjxzwDB3DZUjs44",
  3. "templateType": "3",
  4. "url": "www.liliangel.cn",
  5. "first": {
  6. "value": "测试发送模板消息3",
  7. "color": "#173177"
  8. },
  9. "keyword1": {
  10. "value": "testCar",
  11. "color": "#173177"
  12. },
  13. "keyword2": {
  14. "value": "testPro",
  15. "color": "#173177"
  16. },
  17. "keyword3": {
  18. "value": "successful",
  19. "color": "#173177"
  20. },
  21. "remark": {
  22. "value": "备注",
  23. "color": "#173177"
  24. }
  25. }

说明:我这里的数据结构是经过了一层json封装的,详细的格式可以参考微信官方文档及模板详情!

 

成功:

到这里我们已经完成了模板消息的接入,具体会不会比常用的推送更好? 会不会取代手机短信? 看怎么去运营,发挥你的想象,将微信提供的服务最大程度利用!

 

版权声明:本文为liliangel原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/liliangel/p/6075082.html