微信公众号开发-----微信模板消息推送

本文只提供代码实现,详细内容介绍请阅读信公众平台技术文档之模板消息接口

前提:
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啦
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章