赞
踩
1 发送一次性订阅消息代码
- /**
- * 发送一次性订阅消息
- */
- public static String sendWxAppMessage(String openId, String templateId, JSONObject json, String jumpAddress) {
- String url = Constants.WECHAT_GET_TOKEN + Constants.WECHAT_APPID + appId + Constants.WECHAT_SECRET + secret;
- String access_token = "";
- String code = "";
- try (Response response = OkHttpUtil.get(url)) {
- log.info("接口调用凭证:response=" + response.toString());
- if (response.isSuccessful()) {
- JSONObject resData = JSON.parseObject(response.body().string());
- Integer errcode = resData.getInteger("errcode");
- if (errcode == null || errcode == 0) {
- access_token = resData.getString("access_token");
- if (StringUtils.isNotEmpty(access_token)) {
- log.info("接口调用凭证:access_token =" + access_token);
- String sendUrl = Constants.WECHAT_SEND + access_token;
- JSONObject param = new JSONObject();
- param.put("touser", openId);
- param.put("template_id", templateId);
- param.put("page", jumpAddress);
- param.put("miniprogram_state", "trial");
- param.put("lang", "zh_CN");
- param.put("data", json);
- log.info("请求参数:" + param);
- Response msgResponse = OkHttpUtil.postJson(sendUrl, null, param.toJSONString());
- JSONObject msgJson = JSONObject.parseObject(msgResponse.body().string());
- code = msgJson.getString("errcode");
- log.info("返回结果:" + code);
- }
- } else {
- log.error("获取token失败:errcode = " + errcode);
- return errcode.toString();
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return code;
- }
2 设备订阅消息推送
2.1 官方文档:小程序设备消息 | 微信开放文档
2.2 具体代码
- /**
- * @description:
- * @param: openIds 小程序openid集合
- * templateId 消息模板id
- * modelId 设备modelId
- * data 消息模板数据
- * sn 设备sn码
- */
- public static String sendWxAppDeviceMessage(List<String> openIds, String templateId, String modelId, Object data, String sn) {
- //获取access_token
- String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=?&secret=?";
- String access_token = "";
- String code = "";
- try (Response response = OkHttpUtil.get(url)) {
- log.info("接口调用凭证:response=" + response.toString());
- if (response.isSuccessful()) {
- JSONObject resData = JSON.parseObject(response.body().string());
- Integer errcode = resData.getInteger("errcode");
- if (errcode == null || errcode == 0) {
- access_token = resData.getString("access_token");
- log.info("token:" + access_token);
- String sendUrl = "https://api.weixin.qq.com/cgi-bin/message/device/subscribe/send?access_token=" + access_token;
- JSONObject param = new JSONObject();
- param.put("template_id", templateId);
- param.put("sn", sn);
- //param.put("page","");
- param.put("to_openid_list", openIds);
- param.put("modelId", modelId);
- param.put("data", data);
- param.put("lang", "zh_CN");
- Response msgResponse = OkHttpUtil.postJson(sendUrl, null, param.toJSONString());
- JSONObject msgJson = JSONObject.parseObject(msgResponse.body().string());
- log.info("返回结果:" + msgJson);
- return "success";
- } else {
- log.error("获取token失败:errcode = " + errcode);
-
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return "error";
- }
-
- /**
- * 获取设备票据
- */
- public static AjaxResult getSnTicket(String modelId, String sn) {
- String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=?&secret=?";
- String access_token = "";
- try (Response response = OkHttpUtil.get(url)) {
- log.info("接口调用凭证:response=" + response.toString());
- if (response.isSuccessful()) {
- JSONObject resData = JSON.parseObject(response.body().string());
- Integer errcode = resData.getInteger("errcode");
- if (errcode == null || errcode == 0) {
- access_token = resData.getString("access_token");
- String sendUrl = "https://api.weixin.qq.com/wxa/getsnticket?access_token=" + access_token;
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("sn", sn);
- jsonObject.put("model_id", modelId);
- Response msgResponse = OkHttpUtil.postJson(sendUrl, null, jsonObject.toJSONString());
- JSONObject msgJson = JSONObject.parseObject(msgResponse.body().string());
- if (msgJson.getString("errcode").equals("0")) {
- return AjaxResult.success(msgJson.getString("sn_ticket"));
- }
- return AjaxResult.error();
- }
- }
- } catch (Exception ignored) {
-
- }
- return AjaxResult.error();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。