微信推送模板消息

微信推送模板消息

从来没做过微信和小程序这块,今天写了推送模板消息,因为自己的不细心掉到了坑里半天,写出来分享给大家,希望帮助到小伙伴们。

微信官网文档:

https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/template-message.html

注:在此之前已经获取到了用户的openId和prepay_id、form_id

直接给大家上代码吧,然后介绍下其中需要注意的地方:

准备参数的阶段

这块代码是调用前的准备阶段:

	//调推送模板消息方法
	Map<String, Object> data=new HashMap<String, Object>();
	//data里面的参数1车辆号码
	Map<String,Object> paramsOneDataMap = new HashMap<String,Object>();
	paramsOneDataMap.put("value", carEntity.getPlateNumber());
	//data里面的参数数据2保费金额
	Map<String,Object> paramsTwoDataMap = new HashMap<String,Object>();
	paramsTwoDataMap.put("value","");
	//data里面的参数数据3被保险人(车辆所属人)
	Map<String,Object> paramsThreeDataMap = new HashMap<String,Object>();
	paramsThreeDataMap.put("value", carEntity.getBelonger());
	data.put("keyword1", paramsOneDataMap);
	data.put("keyword2", paramsTwoDataMap);
	data.put("keyword3", paramsThreeDataMap);
	Map<String, Object> resultMap=this.issuePushMessage(openId,wxOrderNo, data);

代码解说

openId:就是官方文档中的touser,接收者(用户)的 openid,每个微信用户都有这个id。
wxOrderNo:官方文档中的form_id对应的值。
data:这个是准备阶段最重要的,官方文档要求传的每个参数都是对象的形式,所以每个我们都要新建一个map存放它们,最后再拿一个大的map把他们装起来,并且一定要记得map的每个键都要和官网的一一对应,不可更改

正式调用

下面就到了我们正式调用的环节了,拿着我们的这些参数,上代码啦:


public Map<String, Object> issuePushMessage(String touser, String prepayId, Map<String,Object> data) {
		
		System.out.println("进入issuePushMessage方法--------------------------------------------------------------------------------");
		
		//获取access_token,参数可在「微信公众平台 - 设置 - 开发设置」页中获得
		String wxspAppid = "小程序唯一凭证,即 AppID";
		String wxspSecret = "小程序唯一凭证密钥,即 AppSecret";
		String params = "appid=" + wxspAppid + "&secret=" + wxspSecret + "&grant_type=client_credential";	
		String accessResult = this.sendGet("https://api.weixin.qq.com/cgi-bin/token", params);
		Gson gson = new Gson();
		Map<String, Object> accessMap = new HashMap<String, Object>();
		accessMap = gson.fromJson(accessResult, accessMap.getClass());
		String accessToken = (String) accessMap.get("access_token");
		
		//传入的参数
		Map<String,Object> paramsTotalMap = new HashMap<String,Object>();
		//接收者(用户)的 openid
		paramsTotalMap.put("touser", touser);
		//所需下发的模板消息的id
		paramsTotalMap.put("template_id", "模板消息id");
		//表单提交场景下,为 submit 事件带上的 form_id;支付场景下,为本次支付的 prepay_id
		paramsTotalMap.put("form_id", prepayId);
		//模板消息显示的数据
		paramsTotalMap.put("data", data);
		String jsonObject = JSONObject.fromObject(paramsTotalMap).toString();
		
		String result = this.sendPost(
				"https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=" + accessToken, jsonObject);
		
		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap = gson.fromJson(result, returnMap.getClass());
		
		return returnMap;
	
	}

代码解说

首先需要获取我们的小程序的accessToken,具体方法看代码注释或官方文档,这块不过多解释。

紧接着就是我们准备工作的参数,最重要的来了啊,我就是在这块入的坑,form_id:它对应两种方式的值,一种是表单提交的formid,另外一种是本次支付的eprepay_id,但是无论你给出的值是哪个,他的键都必须是form_id,切记。 仔细读官方给的文档(表单提交场景下,为 submit 事件带上的 form_id;支付场景下,为本次支付的 prepay_id)。

注释: 模板id,模板基本都是前端直接创建好的,然后会直接提供给后台,包括你的参数顺序也是前端一起提供给你的,如果需要自己创建可参考微信官方文档。所谓的模板消息就是每次给用户推送消息时候显示的数据模板。

返回值问题

还有最重要的 一点,就是返回值问题,这块也烧脑了一会,因为我的sendPost方法返回的是String类型,而官方文档给我们返回的消息是一个对象,所以需要用gson转一下,就是这,转完返回的值类型就变了,文档给出的成功返回值errcode对应的是int类型的0,但是转完之后得到的0.0,所以sendPost不建议大家直接用,仅供参考,我也需要修改下。

sendGet
	public  String sendGet(String url, String param) {
		String result = "";
		BufferedReader in = null;
		try {
			String urlNameString = url + "?" + param;
			URL realUrl = new URL(urlNameString);
			// 打开和URL之间的连接
			URLConnection connection = realUrl.openConnection();
			// 设置通用的请求属性
			connection.setRequestProperty("accept", "*/*");
			connection.setRequestProperty("Charset", "UTF-8");
			connection.setRequestProperty("connection", "Keep-Alive");
			connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			// 建立实际的连接
			connection.connect();
			// 获取所有响应头字段
			Map<String, List<String>> map = connection.getHeaderFields();
			// 遍历所有的响应头字段
			for (String key : map.keySet()) {
				System.out.println(key + "--->" + map.get(key));
			}
			// 定义 BufferedReader输入流来读取URL的响应
			in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
			System.out.println("发送GET请求出现异常!" + e);
			e.printStackTrace();
		}
		// 使用finally块来关闭输入流
		finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return result;
	}

sendPost

  public String sendPost(String url, String param) {
		
		PrintWriter out = null;
		BufferedReader in = null;
		String result = "";
		try {
			URL realUrl = new URL(url);
			// 打开和URL之间的连接
			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)");
			// 发送POST请求必须设置如下两行
			conn.setDoOutput(true);
			conn.setDoInput(true);
			// 获取URLConnection对象对应的输出流
			//out = new PrintWriter(conn.getOutputStream());
			// 发送请求参数
			if(param == null) {
				param = "";
			}
			out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"utf-8"));
			out.print(param);
			// flush输出流的缓冲
			out.flush();
			// 定义BufferedReader输入流来读取URL的响应
			in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
			System.out.println("发送 POST 请求出现异常!" + e);
			e.printStackTrace();
		}
		// 使用finally块来关闭输出流、输入流
		finally {
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return result;
	}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值