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

微信公众号模板消息推送--测试

时间:2018-09-11 11:57:12

相关推荐

微信公众号模板消息推送--测试

1、登录微信公众号平台,进入测试账号

2、可以看到appid和secret

3、添加测试用户

4、手动设置模板消息模式

时间:{{time.DATA}} 申请人:{{reqestion_user.DATA}} 审批人:{{app_user.DATA}} 任务:{{task.DATA}} 单据编号:{{billnumber.DATA}}

5、代码

import com.alibaba.fastjson.JSONObject;import org.weixin4j.Configuration;import org.weixin4j.Weixin;import org.weixin4j.WeixinException;import org.weixin4j.WeixinSupport;import org.weixin4j.http.HttpsClient;import org.weixin4j.http.Response;import org.weixin4j.model.base.Token;import java.text.SimpleDateFormat;import java.util.Date;public class WeixinMessagePush extends WeixinSupport {//测试的https://mp./debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/indexprivate static String appid = "wxa64eaxxxxx";private static String secret = "94682749f2xxxxxxxxxxxxxxxxxx";public static void main(String[] ages)throws Exception{Weixin weixin = new Weixin(appid, secret);Token token = weixin.getToken();WeixinMessagePush w = new WeixinMessagePush();w.templateMessagePush(token.getAccess_token(),"opbb9vi6_aVxxxxxxxxxxxxxxx");}public void templateMessagePush(String token, String openId) throws WeixinException {JSONObject json = new JSONObject();JSONObject text = new JSONObject();JSONObject time = new JSONObject();JSONObject reqestion_user = new JSONObject();JSONObject app_user = new JSONObject();JSONObject billnumber = new JSONObject();JSONObject task = new JSONObject();json.put("touser", openId);json.put("template_id", "i9hGkKOQxuuB6v2OgIglUEQ7_N1YPlDVdg-Wu0MEPAA");json.put("url", ""); //填写跳转地址即可,json.put("topcolor", "#ff1a75");app_user.put("value", "审批人张三123");app_user.put("color","#007f80");time.put("value", "2033-11-22");time.put("color", "#007f80");reqestion_user.put("value", "申请人张思思");reqestion_user.put("color","#007f80");task.put("value", "你有一张单子需要审批");task.put("color", "#007f80");billnumber.put("value", "clsq-123456789");billnumber.put("color", "#007f80");text.put("time", getCurrentTime());text.put("reqestion_user", reqestion_user);text.put("app_user", app_user);text.put("task", task);text.put("billnumber", billnumber);json.put("data", text);//创建请求对象HttpsClient http = new HttpsClient();Response res = http.post("https://api./cgi-bin/message/template/send?access_token=" + token, json);//根据请求结果判定,是否验证成功JSONObject jsonObj = res.asJSONObject();if (jsonObj != null) {if (Configuration.isDebug()) {System.out.println("模板消息返回json:" + jsonObj.toString());}Object errcode = jsonObj.get("errcode");if (errcode != null && !errcode.toString().equals("0")) {//返回异常信息throw new WeixinException(getCause(Integer.parseInt(errcode.toString())));}}}public String getCurrentTime(){SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");return sdf.format(new Date());}}

备注:

1、使用的包

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

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