欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

微信公众号开发,java实现微信公众号模版消息推送

程序员文章站 2024-03-15 14:32:17
...

java实现微信公众号模版消息推送

准备

登录微信公众号后台(公众号模版消息推送必须为认证后的服务号)获取获取添加模版消息ID 

具体实现

java代码

/**
	 * 体脂数据推送信息给微信
	 * @param access_token
	 * @param open_id
	 * @param highPressure
	 * @param lowPressure
	 * @param heart
	 * @param bloodPressureLevel
	 */
	public  void sendBodyCompostionInfoToWeChart(final String access_token,final String open_id,final String user_name,final String bodyCompostionText,final String test_time){

			try {
				String	regUser = "http://wxdemo.xxxx.com/wxdevices/bodycompositionmain?openId="+open_id;
				String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+access_token;
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("touser", open_id); //推送用户openid
				map.put("template_id", WXUtil.TTJK_TEMP_TIZHI_ID); //指定模版ID
				map.put("url", regUser);  //点击模版跳转地址
				Map<String, Object> data_map =new HashMap<String, Object>();
				Map<String, Object> first = new HashMap<String, Object>();
				first.put("value", "您好,你有一条体脂检测信息");
				first.put("color", "#000000");
				Map<String, Object> keynote1 = new HashMap<String, Object>();
				keynote1.put("value", user_name);
				keynote1.put("color", "#000000");
				Map<String, Object> keynote2 = new HashMap<String, Object>();
				keynote2.put("value", test_time);
				keynote2.put("color", "#000000");
				Map<String, Object> keynote3 = new HashMap<String, Object>();
				keynote3.put("value", bodyCompostionText);
				keynote3.put("color", "#000000");
				Map<String, Object> remark = new HashMap<String, Object>();
				remark.put("value", "点击查看详情");
				remark.put("color", "#000000");
				data_map.put("first", first);
				data_map.put("keyword1", keynote1);
				data_map.put("keyword2", keynote2);
				data_map.put("keyword3", keynote3);
				data_map.put("remark", remark);
				map.put("data", data_map);
				JSONObject json = JSONObject.fromObject(map);
				String result = HttpUtil.sendPost(url, json.toString());
				System.out.println(result);
				JSONObject json_arr = JSONObject.fromObject(result);
				String isOk = String.valueOf(json_arr.get("errmsg"));
				logger.info(" errmsg >>>>>>>>> "+isOk);
				if(isOk.equals("ok")){
					logger.info("体脂据推送成功 , "+open_id );
				}else{
					logger.info("体脂据推送失败 , "+open_id );
				}
			} catch (Exception e) {
				logger.error("", e);
			}
			
	}

 成功截图

微信公众号开发,java实现微信公众号模版消息推送

就是这么简单,如有不明白可以点击博客左边+ wx 帮你解答