簡介
模板消息僅用於公衆號向用戶發送重要的服務通知,只能用於符合其要求的服務場景中,如信用卡刷卡通知,商品購買成功通知等。不支持廣告等營銷類消息以及其它所有可能對用戶造成騷擾的消息。
關於使用規則,請注意:
1、所有服務號都可以在功能->添加功能插件處看到申請模板消息功能的入口,但只有認證後的服務號纔可以申請模板消息的使用權限並獲得該權限;
2、需要選擇公衆賬號服務所處的2個行業,每月可更改1次所選行業;
3、在所選擇行業的模板庫中選用已有的模板進行調用;
4、每個賬號可以同時使用25個模板。
5、當前每個賬號的模板消息的日調用上限爲10萬次,單個模板沒有特殊限制。【2014年11月18日將接口調用頻率從默認的日1萬次提升爲日10萬次,可在MP登錄後的開發者中心查看】。當賬號粉絲數超過10W/100W/1000W時,模板消息的日調用上限會相應提升,以公衆號MP後臺開發者中心頁面中標明的數字爲準。
關於接口文檔,請注意:
1、模板消息調用時主要需要模板ID和模板中各參數的賦值內容;
2、模板中參數內容必須以”.DATA”結尾,否則視爲保留字;
3、模板保留符號”{{ }}”。
======================只有認證後的服務號纔可以申請模板消息的使用權限並獲得該權限====
PS:=============請查閱JAVA微信公衆號開發第1篇之環境配置與開發接入進行基本微信接入配置============
申請並添加模板
上傳微信素材
使用wxService.mediaUpload(mediaType, fileType, inputStream)方法
/**
* <p>Title: pushGoOnlineMsg</p>
* <p>Description: 功能上線通知</p>
* @param touser 用戶
* @param url 鏈接
* @param tiltle 標題
* @param functionName 功能名稱
* @param remark 備註
* @return
*/
public Message<TemplateSenderResult> pushGoOnlineMsg(String touser,String url,String tiltle,String functionName,String remark){
int code=1002;
String msg="發送成功!";
TemplateSenderResult templateSenderResult=null;
try {
TemplateSender sender=new TemplateSender();
sender.setTemplate_id("hTjJXn5M3KEkk4ei7z_YQTBxV1s6bQEPhcvbC1lXL3w");
sender.setTouser(touser);
sender.setUrl(url);
Map<String, Object> data=new HashMap<String, Object>();
data.put("first", new ModelMsgData(tiltle,"#1ba2e6"));
data.put("name", new ModelMsgData(functionName,"#1ba2e6"));
data.put("time", new ModelMsgData(new SimpleDateFormat("yyyy年MM月dd日").format(new Date()),"#1ba2e6"));
data.put("remark", new ModelMsgData(remark,"#1ba2e6"));
sender.setData(data);
try {
templateSenderResult=templateSend(sender);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
code=-6666;
msg="服務器開小差啦!";
}
return MessageUtil.getMessage(templateSenderResult, code, msg, null);
}
public TemplateSenderResult templateSend(TemplateSender sender) throws WxErrorException {
TemplateSenderResult result = null;
String postResult = null;
String url = WxConsts.URL_TEMPLATE_SEND.replace("ACCESS_TOKEN",wxService.getAccessToken());
try {
postResult = post(url, sender.toJson());
result = TemplateSenderResult.fromJson(postResult);
} catch (IOException e) {
throw new WxErrorException("[wx-tools]templateSend failure.");
}
return result;
}
public String post(String url, String params) throws WxErrorException {
return execute(new SimplePostRequestExecutor(), url, params);
}
/**
*
* 向微信端發送請求,在這裏執行的策略是當發生access_token過期時纔去刷新,然後重新執行請求,而不是全局定時請求
*
* @param executor
* @param uri
* @param data
* @return
* @throws WxErrorException
* @throws IOException
*/
public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException{
try {
return executeInternal(executor, uri, data);
} catch (WxErrorException e) {
throw e;
}
}
/**
* 請求執行器
*
* @param executor
* @param uri
* @param data
* @return
* @throws WxErrorException
* @throws IOException
*/
protected synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data)
throws WxErrorException{
try {
return executor.execute(getHttpclient(), uri, data);
} catch (WxErrorException e) {
throw e;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}