微信开发之模板消息

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

 

上一篇讲到了获取和缓存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封装的,详细的格式可以参考微信官方文档及模板详情!

 

成功:

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

 

转载于:https://www.cnblogs.com/liliangel/p/6075082.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值