微信模板消息发送实例
来源:互联网 发布:http的默认端口 编辑:程序博客网 时间:2024/05/24 06:58
微信公众号-服务号或者测试号等可以申请高级接口,本例子是以测试号为基础来写。
测试号中,模版内容可以自定义,别的号是微信官方定义好的,不可以修改,订阅号申请不到该接口。
首先定义两个实体类(完成的实体类)
package com.gg.wx.pojo;import java.util.ArrayList;import java.util.List;import com.gg.wx.util.CommonUtil;import com.gg.wx.util.ConstantsActionGo;/** * 模板消息实体类 * */public class Template { // 消息接收方 private String toUser; // 模板id private String templateId; // 模板消息详情链接 private String url; // 消息顶部的颜色 private String topColor; // 参数列表 private List<TemplateParam> templateParamList; //url 为微信公众号token和模板ID public Template(String ToUser,List<TemplateParam> paras,String type,String name){ this.toUser = ToUser; this.templateId = ConstantsActionGo.TEMPLATE_ID;//消息模板ID this.templateParamList = paras; } public String getToUser() { return toUser; } public String getTemplateId() { return templateId; } public String getUrl() { return url; } public String getTopColor() { return topColor; } public void setTopColor(String topColor) { this.topColor = topColor; } public List<TemplateParam> getTemplateParamList() { return templateParamList; } }
package com.gg.wx.pojo;public class TemplateParam { // 参数名称 private String name; // 参数值 private String value; // 颜色 private String color; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public TemplateParam(String name,String value,String color){ this.name=name; this.value=value; this.color=color; } }
下面就是调用微信模板消息的方法,可以先去微信公众号开发官方网站看一下模板消息开发
package com.gg.wx.util;import java.util.ArrayList;import java.util.List;import net.sf.json.JSONObject;import com.gg.wx.pojo.Template;import com.gg.wx.pojo.TemplateParam;public class TempletUtil { public static boolean sendTemplateMsg(Template template){ boolean flag=false; //url 为模板消息请求接口 和 微信token拼接 String url = ConstantsActionGo.TEMPLATE_API + CommonUtil.getToken(ConstantsActionGo.WECHAT_ACTIONGO_APPID, ConstantsActionGo.WECHAT_ACTIONGO_APPSECRET).getAccessToken(); JSONObject jsonResult=CommonUtil.httpsRequest(url, "POST", toJSON(template)); if(jsonResult!=null){ int errorCode=jsonResult.getInt("errcode"); String errorMessage=jsonResult.getString("errmsg"); if(errorCode==0){ flag=true; System.out.println("模板消息发送成功"); }else{ System.out.println("模板消息发送失败:"+errorCode+","+errorMessage); flag=false; } } return flag; } public static void Template(String ToUser,String type,String name) { List<TemplateParam> paras=new ArrayList<TemplateParam>(); paras.add(new TemplateParam("first","您好,你有新的"+type,"#FF3333")); paras.add(new TemplateParam("keyword1",name,"#0044BB")); paras.add(new TemplateParam("keyword2",type,"#0044BB")); paras.add(new TemplateParam("remark","请尽快处理!","#0044BB")); Template tem = new Template(ToUser,paras,type,name); boolean result=sendTemplateMsg(tem); System.out.println("調用成功"); } public static String toJSON(Template template) { StringBuffer buffer = new StringBuffer(); buffer.append("{"); buffer.append(String.format("\"touser\":\"%s\"", template.getToUser())).append(","); buffer.append(String.format("\"template_id\":\"%s\"", template.getTemplateId())).append(","); //消息连接 //buffer.append(String.format("\"url\":\"%s\"", this.url)).append(","); buffer.append(String.format("\"topcolor\":\"%s\"", template.getTopColor())).append(","); buffer.append("\"data\":{"); TemplateParam param = null; for (int i = 0; i < template.getTemplateParamList().size(); i++) { param = template.getTemplateParamList().get(i); // 判断是否追加逗号 if (i < template.getTemplateParamList().size() - 1){ buffer.append(String.format("\"%s\": {\"value\":\"%s\",\"color\":\"%s\"},", param.getName(), param.getValue(), param.getColor())); }else{ buffer.append(String.format("\"%s\": {\"value\":\"%s\",\"color\":\"%s\"}", param.getName(), param.getValue(), param.getColor())); } } buffer.append("}"); buffer.append("}"); return buffer.toString(); } public static void main(String args[]){ //第一个参数为用户关注微信后的唯一标识OpenId,第二个和第三个参数根据自己需要传递 Template("onvI8w_23T****************","调休申请","武腾坤"); }}
“`
运行结果
方法中用到的CommonUtil 类为微信开发工具类,详细代码请查看柳峰老师的博客。 群发我是用for循环来解决的。。。
阅读全文
0 0
- 微信模板消息发送实例
- 微信发送模板消息
- 发送微信模板消息
- 微信模板消息发送
- 微信发送模板消息
- 微信模板消息发送
- 微信公众平台发送模板消息
- 微信公众号发送模板消息
- 微信开发-发送模板消息
- 微信公众平台发送模板消息
- Java 微信发送模板消息
- Java 微信发送模板消息
- 微信公众号发送模板消息
- 微信公众平台 发送模板消息
- 微信公众平台发送模板消息
- 微信公众号发送模板消息
- php微信模板消息发送
- 微信开发---微信模板消息发送
- Struts 2中<s:bean>标签的用法
- 数据结构学习笔记9--栈和队列的顺序存储结构
- 类的const成员函数
- 12. Integer to Roman
- L2-010. 排座位
- 微信模板消息发送实例
- openstack创建的云主机不能访问外网,不能ping www.baidu.com
- Linux安装java环境
- Unity中让游戏对象消失或隐藏的几种方法
- maven项目中,添加依赖后,出现"Dependency 'xxxx‘ not found"解决过程
- a++ 与 ++a
- 10.16考试总结
- 基础练习 十六进制转十进制
- 第二次作业2.2