微信公众号关注推送,需要先关注公众号,创建推送模板,获取模板id,对应模板参数结合微信开放平台文档开发, 注意推送需要在微信开放平台白名单中设置服务器地址,否则获取token接口报错无法获取token,或者报错需要设置把名单,这样需要把报错返回值中ip加入白名单,还有模板参数,传参方式请核对好,否则会报参数无效,phrase15 这个参数在官方文档中限制五个字及以内,否则推送也会失败,具体参数参考官方文档,微信公众号token,统一设置缓存,否则请求频繁会报错
缺少 RestUtil.get 方法,自己找一个http get请求
client_msg_id 参数 防冲入 改成openid加数据id,区分推送数据
package org.jeecg.modules.util;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.RestUtil;
import org.jeecg.config.sign.util.HttpUtils;
import javax.xml.transform.Result;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
/**
* Demo class
*微信三方工具类
* @author mjl
* @date 2016/10/31
*/
@Slf4j
public class WxTkenUtil {
/**发送模板消息*/
public static String send_urls = "https://api.weixin.qq.com/cgi-bin/message/template/send";
/**获取token认证*/
public static String access_token = "https://api.weixin.qq.com/cgi-bin/token";
/**公众号appid*/
public static String appid = "";
/**公众号密钥*/
public static String secret = "";
/**认证参数*/
public static String grantType = "client_credential";
/**公众号模板id*/
public static String template_id = "xdE8eTjmJISYZsRYbyFmnBP2dRCAo-O38FzAa3wLo1M";
/**获取token*/
public static String getAccessToken(String url,String grantType,String appid,String secret){
String access_token ="";
String tokenUrl = url +"?grant_type="+grantType+"&appid="+appid +"&secret="+ secret;
Object result = RestUtil.get(tokenUrl);
JSONObject jsons = JSONObject.parseObject(result.toString());
log.info("=======================公众号推送获取token:=======================" + jsons.toJSONString());
String expires_in = jsons.getString("expires_in");
if(StringUtils.isNotEmpty(expires_in) && Integer.parseInt(expires_in) == 7200){
access_token = jsons.getString("access_token");
}
return access_token;
}
/**
*date 开课时间,userOpenId 公众号openId,title标题,lecturerName讲师名字,access_token token验证
* 公众号推送消息*/
public static String sendInfo(Date date,String userOpenId,String title,String lecturerName,String access_token,
String templateId,String sendUrls){
JSONObject json = new JSONObject();
json.put("touser",userOpenId);
json.put("template_id",templateId);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年mm月dd日 hh:mm");
String formatDate = simpleDateFormat.format(date);
JSONObject dataJson = new JSONObject();
HashMap<String,Object> timeMap = new HashMap();
timeMap.put("value",formatDate);
dataJson.put("time8",timeMap);
HashMap<String,Object> titleMap = new HashMap();
titleMap.put("value",title);
dataJson.put("thing9",titleMap);
HashMap<String,Object> nameMap = new HashMap();
nameMap.put("value",lecturerName);
dataJson.put("phrase15",nameMap);
HashMap<String,Object> addressMap = new HashMap();
addressMap.put("value",title);
dataJson.put("thing14",addressMap);
json.put("data",dataJson);
json.put("client_msg_id",userOpenId);
String sendresult = RestUtil.sendPost(sendUrls + "?access_token="+access_token, json.toJSONString());
log.info("===================公众号推送消息结果:=================" + sendresult);
return sendresult;
}
}
http post 工具类
/**
* 向指定 URL 发送POST方法的请求
*
* @param url 发送请求的 URL
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public static String sendPost(String url, String param)
{
PrintWriter out = null;
BufferedReader in = null;
StringBuilder result = new StringBuilder();
try
{
String urlNameString = url;
log.info("sendPost - {}", urlNameString);
URL realUrl = new URL(urlNameString);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestProperty("Accept-Charset", "utf-8");
conn.setRequestProperty("contentType", "utf-8");
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String line;
while ((line = in.readLine()) != null)
{
result.append(line);
}
log.info("recv - {}", result);
}
catch (ConnectException e)
{
log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e);
}
catch (SocketTimeoutException e)
{
log.error("调用HttpUtils.sendPost SocketTimeoutException, url=" + url + ",param=" + param, e);
}
catch (IOException e)
{
log.error("调用HttpUtils.sendPost IOException, url=" + url + ",param=" + param, e);
}
catch (Exception e)
{
log.error("调用HttpsUtil.sendPost Exception, url=" + url + ",param=" + param, e);
}
finally
{
try
{
if (out != null)
{
out.close();
}
if (in != null)
{
in.close();
}
}
catch (IOException ex)
{
log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
}
}
return result.toString();
}
以下为引用,自己写demo测一下,不清楚,不喜欢勿喷
String accessToken = WxTkenUtil.getAccessToken(WxTkenUtil.access_token, "client_credential", WxTkenUtil.appid
,WxTkenUtil.secret);
String sendResult = WxTkenUtil.sendInfo(temp.getStartTime(),appuser.getH5OpenId(),
temp.getLiveTitle(), lecturer.getName(),accessToken,WxTkenUtil.template_id,
WxTkenUtil.send_urls);
System.out.println("==================="+sendResult);