微信公众号推送图文消息

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013008827/article/details/88950375

需求

准备工作

代码编写

最终效果

目录

需求

 用户在做完某个操作后,发送图文消息到用户微信公众号显示提醒

准备工作

  公众号为服务号

  获取公众号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();
		}
	}

最终效果


猜你喜欢

转载自blog.csdn.net/u013008827/article/details/88950375