本文只提供代碼實現,詳細內容介紹請閱讀信公衆平臺技術文檔之模板消息接口
前提:
1)獲取用戶openId
微信公衆號開發-----網頁授權,java獲取微信公衆號用戶的個人信息
2)獲取access_token並定時刷新
微信公衆號開發----獲取access_token,定時刷新access_token
1、登錄微信公衆平臺,在模板庫中添加想要的模板
2、發送消息模板說明
接口調用請求說明
http請求方式: POST https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN
POST數據說明
POST數據示例如下:
{
"touser":"OPENID",
"template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
"url":"http://weixin.qq.com/download",
"miniprogram":{
"appid":"xiaochengxuappid12345",
"pagepath":"index?foo=bar"
},
"data":{
"first": {
"value":"恭喜你購買成功!",
"color":"#173177"
},
"keyword1":{
"value":"巧克力",
"color":"#173177"
},
"keyword2": {
"value":"39.8元",
"color":"#173177"
},
"keyword3": {
"value":"2014年9月22日",
"color":"#173177"
},
"remark":{
"value":"歡迎再次購買!",
"color":"#173177"
}
}
}
參數說明可參閱技術文檔,其中date爲模板數據,可點擊詳細查看所選中的模板詳情
後面的代碼將根據這個“設備綁定”模板來實現
3、代碼實現
1)創建實體(省略set/get方法)
/**
* 模板消息推送
*/
public class TemplateMsgPush {
private String touser;
@JSONField(name = "template_id")
private String templateId;
private String url;
private JSONObject data;
}
2)組建模板內容
/**
* 設備消息推送
*/
public class DeviceMsgPush {
/**
* 設備綁定通知
* @param accessToken
* @param url 點擊’詳細’時的跳轉鏈接
* @param user
* @param sn 設備號
* @param templateId 模板id
* @return
*/
public static Integer deviceBindPush(String accessToken, String url, User user, String sn,String templateId){
JSONObject first=new JSONObject();//消息的首行,標題
first.put("value","設備綁定成功!");
first.put("color","#E36C0A");
JSONObject keyword1=new JSONObject();
JSONObject keyword2=new JSONObject();
JSONObject keyword3=new JSONObject();
Date date = new Date();
DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
keyword1.put("value", df.format(date));
keyword2.put("value",sn);
keyword3.put("value",user.getNickname());
JSONObject remark=new JSONObject();
remark.put("value", "點擊“詳情”,填寫個人信息,完成初始設置。");
remark.put("color", "#E36C0A");//顏色設置
JSONObject text=new JSONObject();
text.put("keyword1", keyword1);
text.put("keyword2", keyword2);
text.put("keyword3", keyword3);
text.put("first", first);
text.put("remark",remark);
TemplateMsgPush msgPush = new TemplateMsgPush();
msgPush.setTouser(user.getWcId());//用戶openId
msgPush.setTemplateId(templateId);//模板id
msgPush.setUrl(url);
msgPush.setData(text);
//發送請求
WechatUtil.push(accessToken,msgPush);
}
}
3)發送
其中用於發送POST或GET請求的工具:HttpsUtil類
public class WechartConst {
//發送消息模板
public static final String SEND_MESSAGE = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
}
/**
* 推送模板消息
*/
public class WechatUtil {
private static Logger log = LogManager.getLogger(WechatUtil.class);
public static void push(String accessToken, TemplateMsgPush msgPush){
String requestUrl = WechartConst.SEND_MESSAGE.replace("ACCESS_TOKEN",accessToken);
JSONObject jsonObject = HttpsUtil.request(requestUrl, "POST", JSON.toJSONString(msgPush));
Integer errCode = jsonObject.getInteger("errcode");
if (errCode != 0){
log.warn("發送模板消息失敗:"+jsonObject.toJSONString());
}
}
}
4)使用示例
調用DeviceMsgPush.deviceBindPush()方法,傳入參數就OK啦