失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > java微信推送模板销消息(微信测试号)

java微信推送模板销消息(微信测试号)

时间:2021-05-05 16:11:19

相关推荐

java微信推送模板销消息(微信测试号)

java微信推送模板销消息(微信测试号)

相关jar包(红框内)

如果仅需推送模板消息,不对用户动作进行处理的话不需进行服务器配置

下面的操作皆借助于微信的测试号,点击下方链接进入登录页,微信扫描即可进入

测试号链接:点击进入测试号

1. 新建模板

此处为测试号,正式上线模板只能选择,不能自定义,如需自定义则需申请

标题自定义内容按微信给出的格式输入,可用下面的例子

{{title.DATA}}登记时间: {{registrationDate.DATA}}缺货数量: {{shortageQuantity.DATA}}发货基地: {{deliveryBase.DATA}}责任部门: {{responsibleDepartment.DATA}}备注: {{remark.DATA}}

2. 新建模板实体

根据微信给出的接口说明建消息类

import lombok.AllArgsConstructor;import lombok.Builder;import lombok.Data;import lombok.NoArgsConstructor;import java.util.Map;@AllArgsConstructor@NoArgsConstructor@Data@Builderpublic class WxTemplate {private String touser;private String template_id;private String url;private Map<String, TemplateData> data;}

3. 发送消息

建get和post请求的工具类

import org.apache.http.HttpEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.ContentType;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import java.nio.charset.StandardCharsets;public class HttpUtil {/*** 模拟get请求* @param url* @return*/public static String sendGet(String url) {String response = null;CloseableHttpClient aDefault = HttpClients.createDefault();HttpGet httpGet = new HttpGet(url);try {CloseableHttpResponse execute = aDefault.execute(httpGet);response = EntityUtils.toString(execute.getEntity(), StandardCharsets.UTF_8);} catch (Exception e) {e.printStackTrace();}finally {if (aDefault != null) {try {aDefault.close();} catch (Exception e) {e.printStackTrace();}}}return response;}/**** @param url "http://127.0.0.1:8083/shortageInfo/getSingle"* @param data {'shortageInfo': {'id':'105'}}* @param mimeType mimeType* @param charset charset* @return response*/public static String sendPost(String url, String data, String mimeType, String charset) {String response = null;try {CloseableHttpClient httpclient = null;CloseableHttpResponse httpResponse = null;try {httpclient = HttpClients.createDefault();HttpPost httppost = new HttpPost(url);HttpEntity stringentity = new StringEntity(data, ContentType.create(mimeType, charset));httppost.setEntity(stringentity);httpResponse = httpclient.execute(httppost);response = EntityUtils.toString(httpResponse.getEntity());System.out.println(response);} finally {if (httpclient != null) {httpclient.close();}if (httpResponse != null) {httpResponse.close();}}} catch (Exception e) {e.printStackTrace();}return response;}/*** 模拟post请求* @param url url* @param data data* @return 返回对象字符串*/public static String sendPost(String url, String data) {return sendPost(url, data, "application/json", "UTF-8");}}

发送消息

import com.ak.job.entity.WxTemplate;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import lombok.SneakyThrows;import lombok.extern.slf4j.Slf4j;@Slf4jpublic class MyTest {public static final String APP_ID = "wx38ec2eb5f13";public static final String APP_SECRET = "bf0f36dd83799be52f";public static final String ACCESS_TOKEN_URL = "https://api./cgi-bin/token?grant_type=client_credential&appid=" + APP_ID + "&secret=" + APP_SECRET;//模板IDpublic static final String TEMPLATE_ID="jSuFd42IenE24OlhigirEVJKmsHNjUPaRa01XKsIMCk";//模板消息详情跳转URLpublic static final String TO_URL="";public static final String SEND_TEMPLATE_URL = "https://api./cgi-bin/message/template/send?access_token=%s";public static void executePush(WxTemplate wxTemplate, String accessToken) {Object o = JSON.toJSON(wxTemplate);String s = HttpUtil.sendPost(String.format(SEND_TEMPLATE_URL, accessToken, o.toString().replace("[", "{").replace("]", "}"));System.out.println(s);}@SneakyThrowspublic static void main(String[] args) {// 获取access_tokenString s = HttpUtil.sendGet(ACCESS_TOKEN_URL);JSONObject jsonObject = JSON.parseObject(s);String accessToken = jsonObject.getString("access_token");// 构建消息模板Map<String, TemplateData> templateData = new HashMap<>();templateData.put("title", TemplateData.builder().value("默认标题").color("#000000").build()); // titletemplateData.put("shortageQuantity", TemplateData.builder().value("1000").color("#ff0000").build()); // shortageQuantitytemplateData.put("responsibleDepartment", TemplateData.builder().value("默认部门").color("#ff0045").build()); // responsibleDepartmenttemplateData.put("deliveryBase", TemplateData.builder().value("默认基地").color("#000000").build()); // deliveryBasetemplateData.put("registrationDate", TemplateData.builder().value(System.currentTimeMillis() / 10 + "").color("#000000").build()); // registrationDatetemplateData.put("remark", TemplateData.builder().value("默认备注").color("#000000").build()); // remarkWxTemplate wxTemplate = WxTemplate.builder().template_id(TEMPLATE_ID).data(templateData).url(TO_URL).touser("ojx0a5i32V7AuMiKd5Z4Kjthik1k").build();}executePush(wxTemplate, accessToken); // 发送消息}

如果觉得《java微信推送模板销消息(微信测试号)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。