您的位置:首页 > 移动开发 > 微信开发

微信公众账号推送模板消息(java版)

2017-03-01 15:57 711 查看
此文主要用于微信公众平台需要推送模板消息给关注公众号的客户。

微信公众平台模板消息技术文档地址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842&token=&lang=zh_CN

微信公众平台配置:

1、添加 模板消息 功能插件



注意事项:设置模板行业(每个行业有特定的模板格式,根据需要设置所属行业),因为模板格式一个月只能修改一次。

具体开放步骤:

1、获取access_token  (https get )

请求地址:https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=&secret=;

参数:appid ,secret (在公众平台 开发--> 基本配置 可以看到)

返回json:

{
"access_token": "",
"expires_in": 7200
}


2、获取微信公众号行业信息 (https get )

请求地址:https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=

返回json:

{
"primary_industry":{"first_class":"运输与仓储","second_class":"快递"},
"secondary_industry":{"first_class":"IT科技","second_class":"互联网|电子商务"}
}


3、获取模板template_id (https post)

请求地址:https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=

参数:

{
"template_id_short":"TM00015"
}


返回:

{
"errcode":0,
"errmsg":"ok",
"template_id":"Doclyl5uP7Aciu-qZ7mJNPtWkbkYnWBWVja26EGbNyk"
}


4、获取公众号模板列表(https get)

请求地址:https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=

参数:无

返回json:

{
"template_list": [{
"template_id": "iPk5sOIt5X_flOVKn5GrTFpncEYTojx6ddbt8WYoV5s",
"title": "领取奖金提醒",
"primary_industry": "IT科技",
"deputy_industry": "互联网|电子商务",
"content": "{ {result.DATA} }\n\n领奖金额:{ {withdrawMoney.DATA} }\n领奖  时间:{ {withdrawTime.DATA} }\n银行信息:{ {cardInfo.DATA} }\n到账时间:  { {arrivedTime.DATA} }\n{ {remark.DATA} }",
"example": "您已提交领奖申请\n\n领奖金额:xxxx元\n领奖时间:2013-10-10 12:22:22\n银行信息:xx银行(尾号xxxx)\n到账时间:预计xxxxxxx\n\n预计将于xxxx到达您的银行卡"
}]
}


参数 是否必填 说明

access_token 是
接口调用凭证

template_id 是
模板ID

title 是 模板标题

primary_industry 是
模板所属行业的一级行业

deputy_industry 是
模板所属行业的二级行业

content 是 模板内容

example 是 模板示例

5.发送消息(https post)

请求地址:https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=

post 参数:

{
"touser":"OPENID",
"template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
"url":"http://weixin.qq.com/download",
"data":{
"first": {
"value":"恭喜你购买成功!",
"color":"#173177"
},
"keynote1":{
"value":"巧克力",
"color":"#173177"
},
"keynote2": {
"value":"39.8元",
"color":"#173177"
},

a079
"keynote3": {
"value":"2014年9月22日",
"color":"#173177"
},
"remark":{
"value":"欢迎再次购买!",
"color":"#173177"
}
}
}


public class CreateJson {

private static Map<String, Object> map;

private static Map<String, Object> data;

/**
* 发送<订单支付成功>模板消息
*
* @see 3K8-aqG4nLLyV5GLnh4JENBKyi8A5XRacWRML5SoQXA (模板ID)
* @param openid
*            接收用户OPENID
* @param url
*            点击模板后跳转地址
* @return JSONObject
*/
public static JSONObject create_pay_success_Json(String openid, String url) {

// 模板id
String template_id = "3K8-aqG4nLLyV5GLnh4JENBKyi8A5XRacWRML5SoQXA";

// 模板的主题颜色
String topcolor = "#008000";

// 用户昵称
String username = WxGetUserUtil.getUserData(openid);

// 构造json包

map = new LinkedHashMap<String, Object>();

data = new LinkedHashMap<String, Object>();

LinkedHashMap<String, String> first = new LinkedHashMap<String, String>();

first.put("value", "尊敬的" + username + ":\n\n您尾号为0426的招商银行卡最近有一笔交易(测试)");

first.put("color", "#743A3A");

data.put("first", first);

LinkedHashMap<String, String> keyword1 = new LinkedHashMap<String, String>();

keyword1.put("value", "YXJ134953845");

keyword1.put("color", "#FF0000");

data.put("keyword1", keyword1);

LinkedHashMap<String, String> keyword2 = new LinkedHashMap<String, String>();

keyword2.put("value", "2014/08/18 13:18");

keyword2.put("color", "#C4C400");

data.put("keyword2", keyword2);

LinkedHashMap<String, String> remark = new LinkedHashMap<String, String>();

remark.put("value", "\n\n截止2014/08/18 13:18您招商信用账户可用余额未20000元");

remark.put("color", "#000000");

data.put("remark", remark);

map.put("touser", openid);

map.put("template_id", template_id);

map.put("url", url);

map.put("topcolor", topcolor);

map.put("data", data);

// TradingNotice wn = new TradingNotice(openid, template_id,
// url,topcolor, username);

JSONObject jsonObject = JSONObject.fromObject(map);

return jsonObject;
}
}


返回内容:

  {
           "errcode":0,
           "errmsg":"ok",
           "msgid":200228332
       }


以上所有代码都是经过测试,并且通过的。放心使用。转载请注明出处。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息