微信公众号推送图文消息
需求
准备工作
代码编写
最终效果
目录
用户在做完某个操作后,发送图文消息到用户微信公众号显示提醒
公众号为服务号
获取公众号token
public static void main(String[] args) {
String openId = "od9apwdSsaFOpTOr64d8HLsUY3Zc";
String token="14_W9bKeI1tyBEO6MvAky821XRWokVfSgY2ke3m1FZXrRpC0Ko9fElwyssRegxbtGOHh4bFqsnDlJ6btgmE6f9LYokdka6wU2ehfsj-Qk-Y8JyeKabYDczN9kBoTU8BWJgAEAPKX";
String url = "http://www.baidu.com/";
String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN";
requestUrl = requestUrl.replace("ACCESS_TOKEN", token); // 发送客服消息
JSONObject jsonObject = new JSONObject();
jsonObject.put("msgtype", "news");
jsonObject.put("touser", openId);
JSONObject jsonContent = new JSONObject();
jsonContent.put("title", "您好!请点击查看报告");
jsonContent.put("description", "检测时间:"+ new Date());
jsonContent.put("url", url);
jsonContent.put("picurl", "http://www.jiankangzhan.com/img/coupon10.png");
JSONArray articles = new JSONArray();
articles.add(jsonContent);
JSONObject articlesO = new JSONObject();
articlesO.put("articles", articles);
jsonObject.put("news", articlesO);
String jsonStr;
try {
jsonStr = HttpUtils.sendPostBuffer(requestUrl, jsonObject.toString());
System.out.println(jsonStr);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}