当前位置: 首页 > news >正文

java 微信 推送_JAVA 微信消息模板推送

公众号配置模板示例

推送接口

/**

*

* @title: sendTemplate

* @author: daixiaowei

* @date: 2019年11月09日 下午 12:59:27

* @description: 支付微信模板推送

* @param: fillData

* @return:

*/

public String sendTemplate(String openid,String templateId,String[] fillData,String orderId,String vendorId) {

String orderIds = "";

if (StringUtils.isNotEmpty(orderId)) {

orderIds = orderId;

}

String tepUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="

+ getAccessToken();

String frontUrl = conf.get("weixinpay.msgDetailUrl")+"/"

+vendorId +"/"+orderIds+"/"+openid;

CloseableHttpClient httpClient = HttpClients.createDefault();

HttpPost httpPost = new HttpPost(tepUrl);

// 装配post请求参数

JSONObject json = new JSONObject();

json.put("touser", openid);//消息接收者的openid

json.put("template_id", templateId);//消息模板id

json.put("url", frontUrl);//模板里详情页链接

json.put("emphasis_keyword", "keyword1.DATA");

JSONObject dataJson = new JSONObject();

//数据插入

for (int i = 0; i < fillData.length; i++) {

JSONObject sonDateJson = new JSONObject();

sonDateJson.put("value", fillData[i]);

dataJson.put("keyword" + (i + 1), sonDateJson);

}

json.put("data", dataJson);

String resultStr = "发送失败";

try {

StringEntity myEntity = new StringEntity(json.toJSONString(), "UTF-8");

// 设置post求情参数

httpPost.setEntity(myEntity);

HttpResponse httpResponse = httpClient.execute(httpPost);

if (httpResponse.getStatusLine().getStatusCode() == 200) {

// 发送成功

String resutlEntity = EntityUtils.toString(httpResponse.getEntity());

ResultTemplateDate resultTemplateDate = JSONObject.parseObject(resutlEntity, ResultTemplateDate.class);

if (resultTemplateDate.getErrcode().equals("40001")) {

resultStr = "token失效";

}

if (resultTemplateDate.getErrcode().equals("40037")) {

resultStr = "template_id不正确";

}

if (resultTemplateDate.getErrcode().equals("41030")) {

resultStr = "page不正确";

}

if (resultTemplateDate.getErrcode().equals("45009")) {

resultStr = "接口调用超过限额(目前默认每个帐号日调用限额为10万)";

}

resultStr = "ok";

return resultStr;

} else {

// 发送失败

return resultStr;

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (httpClient != null) {

// 释放资源

httpClient.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

return resultStr;

}

获取token方法

/**

*

* @title: getAccessToken

* @author: daixiaowei

* @date: 2019年11月09日 上午 11:00:33

* @description: 获取token

* @param:

* @return:

*/

public String getAccessToken() {

String tmpUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + conf.get("weixinpay.appid")

+ "&secret=" + conf.get("weixinpay.secret") + "";

CloseableHttpClient httpCilent = HttpClients.createDefault();

HttpGet httpGet = new HttpGet(tmpUrl);

try {

HttpResponse httpResponse = httpCilent.execute(httpGet);

if (httpResponse.getStatusLine().getStatusCode() == 200) {

String entity = EntityUtils.toString(httpResponse.getEntity());

AccessToken accessToken = JSONObject.parseObject(entity, AccessToken.class);

return accessToken.getAccess_token();

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

// 释放资源

httpCilent.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return "";

}

模板返回值类

package com.jkr.common.util;

/**

* 模板返回消息

* @author daixiaowei

*/

public class ResultTemplateDate {

private String errcode;

private String errmsg;

public String getErrcode() {

return errcode;

}

public void setErrcode(String errcode) {

this.errcode = errcode;

}

public String getErrmsg() {

return errmsg;

}

public void setErrmsg(String errmsg) {

this.errmsg = errmsg;

}

}


http://www.taodudu.cc/news/show-6110218.html

相关文章:

  • java 微信多线程推送_Java实现 微信小程序 + 消息推送
  • esp32cam与android app的同步udp通信
  • ESP32 CAM与服务器(python)UDP视频传输
  • vite创建vue保姆级教程
  • 环境变量和模式(Vite)
  • vue axios介绍
  • Cache架构以及X86ARM @Linux平台cache eviction功能测试
  • vue中使用CSS预处理器
  • vue3+vite环境搭建 vue3+vite实战
  • vite和webpack的区别
  • ARM cache 结构与访问方式
  • app提示已到期_有了这个App的提醒,再也不用担心商品什么时候过期啦
  • Matlab App Designer编译打包exe后读取文件路径问题
  • App断网提醒设计
  • 到站提醒APP应用 隐私声明
  • 精品基于Uniapp+Springboot实现的患者服药提醒APP
  • “日历提醒app”程序说明文档
  • 地铁到站提醒APP
  • 数学 {n次方根,根号,平方根}
  • 万能Markdown数学公式
  • Java中开根号,你还记得吗
  • 【数学】-1的根号二次方等于多少?
  • 数据库——关系模型之关系演算(元组演算与域演算)
  • 南航计算机科学与技术学院院徽,南京航空航天大学计算机科学与技术学院简介...
  • RSA2
  • RSA C语言实现
  • 业务中继承关系研究(数据库)
  • 三峡大学计算机与信息学院王,周欢,三峡大学 计算机与信息学院
  • 信息安全-RSA
  • RSA--------------