Preface:
When we purchase products or other operations, the WeChat official account will push related template messages. Next, briefly introduce the development process: (This article takes order push as an example)
First create a new template message in the test number
The format is as follows:
{{first.DATA}} Username: {{keyword1.DATA}} Order number: {{keyword2.DATA}} Order amount: {{keyword3.DATA}} Product information: {{keyword4.DATA}} {{remark.DATA}}A template ID will be generated here, and it will be used later
Then the background uploads the order interface, and after the upload is successful, the template message is sent, the code implementation:
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("Congratulations on your successful purchase!", "#ff0000"); DataInfo keyword1 = new DataInfo(wechatUser.getNickName(), "#ff0000"); DataInfo keyword2 = new DataInfo(order.getOrderNumber(), "#ff0000"); DataInfo keyword3 = new DataInfo("¥" + order.getSumPrice() + "meta", "#ff0000"); DataInfo keyword4 = new DataInfo(goodsInfo, "#ff0000"); DataInfo remark = new DataInfo("Welcome to buy again!", "#000000"); OrderData orderData = new OrderData(first, keyword1, keyword2, keyword3, keyword4, remark); OrderTemplateMessage templateMessage = new OrderTemplateMessage(); templateMessage.setTouser(wechatUser.getId()); templateMessage.setTemplate_id(orderTemplateId);//Template ID templateMessage.setData(orderData); OkHttpUtil.getInstance().doPost(WechatConstant.getTemplateUrl(wechatAccessTokenService.takeAccessToken()), JSON.toJSONString(templateMessage));}A brief introduction to OkHttpUtil
OkHttp is a third-party class library used to request the network in Android.
public String doPost(String url, String param) { MediaType ); 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; }Pay attention to configuring the order template ID
@Value("#{setting.ordertemplateid}") private String orderTemplateId;The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.