微信公众号发送模版消息
2018-03-26 17:07
162 查看
在我们购买商品或其他操作的时候,这时候微信公众号会推送相关模版消息。接下来简单介绍开发流程:(本文以订单推送为例)
首先在测试号新建模版消息
OkHttp是一个第三方类库,用于Android中请求网络。public String doPost(String url, String param) {
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, param);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Call call = okHttpClient.newCall(request);
try {
Response response = call.execute();
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
首先在测试号新建模版消息
格式如下:
{{first.DATA}} 用户名:{{keyword1.DATA}} 订单号:{{keyword2.DATA}} 订单金额:{{keyword3.DATA}} 商品信息:{{keyword4.DATA}} {{remark.DATA}这里会生成一个模版ID,后面会用到
然后后台在上传订单接口,上传成功后调用发送模版消息
public void sendOrderTemplateMessage(Order order) { AugeWechatUser wechatUser = augeWechatUserMapper.selectByPhone(order.getPhone()); String goodsInfo = ""; for (AugeSaleItem augeSaleItem : order.getData()) { goodsInfo += augeSaleItem.getItemName() + "×" + augeSaleItem.getItemNum() + "\n\t\t\t"; } DataInfo first = new DataInfo("恭喜你购买成功!", "#ff0000"); DataInfo keyword1 = new DataInfo(wechatUser.getNickName(), "#ff0000"); DataInfo keyword2 = new DataInfo(order.getOrderNumber(), "#ff0000"); DataInfo keyword3 = new DataInfo("¥" + order.getSumPrice() + "元", "#ff0000"); DataInfo keyword4 = new DataInfo(goodsInfo, "#ff0000"); DataInfo remark = new DataInfo("欢迎再次购买!", "#000000"); OrderData orderData = new OrderData(first, keyword1, keyword2, keyword3, keyword4, remark); OrderTemplateMessage templateMessage = new OrderTemplateMessage(); templateMessage.setTouser(wechatUser.getId()); templateMessage.setTemplate_id(orderTemplateId);//模版ID templateMessage.setData(orderData); OkHttpUtil.getInstance().doPost(WechatConstant.getTemplateUrl(wechatAccessTokenService.takeAccessToken()), JSON.toJSONString(templateMessage)); }简单介绍 OkHttpUtil
OkHttp是一个第三方类库,用于Android中请求网络。public String doPost(String url, String param) {
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, param);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Call call = okHttpClient.newCall(request);
try {
Response response = call.execute();
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
注意配置订单模版ID
@Value("#{setting.ordertemplateid}") private String orderTemplateId;
相关文章推荐
- 微信公众号发送模版消息
- java实现微信公众号发送模版消息
- 微信公众号开发之[发送模板消息]
- Django开发微信公众号(5) -- 实现消息的接收与发送
- 使用shell通过微信公众号发送模板消息
- Python 微信公众号发送消息
- 微信公众号 发送图文消息
- 微信公众平台开发基础功能(获取unionid,发送模版消息等)
- Django开发微信公众号(5) -- 实现消息的接收与发送
- java微信公众号发送消息模板
- 微信公众号获取用户发送的普通消息
- 微信公众号发送文本消息时如何在文本消息中包含超链接
- 微信公众号开发-----微信模板消息接口-----发送模板消息
- 向微信公众号发送么个特地消息给么个人
- 微信公众号发送模板消息
- 主动给用户发消息-微信公众号模版消息
- 微信公众号开发之消息发送
- 微信公众号开发接收图片消息,获取用户发送图片的media_id
- 微信公众号开发(Java) ---- 客服消息发送
- 微信公众号发送消息之发送客服消息基类封装