天天看點

微信公衆号推送圖文消息需求準備工作代碼編寫最終效果

需求

準備工作

代碼編寫

最終效果

目錄

需求

 使用者在做完某個操作後,發送圖文消息到使用者微信公衆号顯示提醒

準備工作

  公衆号為服務号

  擷取公衆号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();
		}
	}
           

最終效果

微信公衆号推送圖文消息需求準備工作代碼編寫最終效果