当前位置:   article > 正文

微信小程序中发送一次性订阅消息和设备长期订阅消息_微信小程序一次性订阅和长期订阅

微信小程序一次性订阅和长期订阅

1 发送一次性订阅消息代码

  1. /**
  2. * 发送一次性订阅消息
  3. */
  4. public static String sendWxAppMessage(String openId, String templateId, JSONObject json, String jumpAddress) {
  5. String url = Constants.WECHAT_GET_TOKEN + Constants.WECHAT_APPID + appId + Constants.WECHAT_SECRET + secret;
  6. String access_token = "";
  7. String code = "";
  8. try (Response response = OkHttpUtil.get(url)) {
  9. log.info("接口调用凭证:response=" + response.toString());
  10. if (response.isSuccessful()) {
  11. JSONObject resData = JSON.parseObject(response.body().string());
  12. Integer errcode = resData.getInteger("errcode");
  13. if (errcode == null || errcode == 0) {
  14. access_token = resData.getString("access_token");
  15. if (StringUtils.isNotEmpty(access_token)) {
  16. log.info("接口调用凭证:access_token =" + access_token);
  17. String sendUrl = Constants.WECHAT_SEND + access_token;
  18. JSONObject param = new JSONObject();
  19. param.put("touser", openId);
  20. param.put("template_id", templateId);
  21. param.put("page", jumpAddress);
  22. param.put("miniprogram_state", "trial");
  23. param.put("lang", "zh_CN");
  24. param.put("data", json);
  25. log.info("请求参数:" + param);
  26. Response msgResponse = OkHttpUtil.postJson(sendUrl, null, param.toJSONString());
  27. JSONObject msgJson = JSONObject.parseObject(msgResponse.body().string());
  28. code = msgJson.getString("errcode");
  29. log.info("返回结果:" + code);
  30. }
  31. } else {
  32. log.error("获取token失败:errcode = " + errcode);
  33. return errcode.toString();
  34. }
  35. }
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. return code;
  40. }

2 设备订阅消息推送

2.1 官方文档:小程序设备消息 | 微信开放文档

2.2 具体代码

  1. /**
  2. * @description:
  3. * @param: openIds 小程序openid集合
  4. * templateId 消息模板id
  5. * modelId 设备modelId
  6. * data 消息模板数据
  7. * sn 设备sn码
  8. */
  9. public static String sendWxAppDeviceMessage(List<String> openIds, String templateId, String modelId, Object data, String sn) {
  10. //获取access_token
  11. String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=?&secret=?";
  12. String access_token = "";
  13. String code = "";
  14. try (Response response = OkHttpUtil.get(url)) {
  15. log.info("接口调用凭证:response=" + response.toString());
  16. if (response.isSuccessful()) {
  17. JSONObject resData = JSON.parseObject(response.body().string());
  18. Integer errcode = resData.getInteger("errcode");
  19. if (errcode == null || errcode == 0) {
  20. access_token = resData.getString("access_token");
  21. log.info("token:" + access_token);
  22. String sendUrl = "https://api.weixin.qq.com/cgi-bin/message/device/subscribe/send?access_token=" + access_token;
  23. JSONObject param = new JSONObject();
  24. param.put("template_id", templateId);
  25. param.put("sn", sn);
  26. //param.put("page","");
  27. param.put("to_openid_list", openIds);
  28. param.put("modelId", modelId);
  29. param.put("data", data);
  30. param.put("lang", "zh_CN");
  31. Response msgResponse = OkHttpUtil.postJson(sendUrl, null, param.toJSONString());
  32. JSONObject msgJson = JSONObject.parseObject(msgResponse.body().string());
  33. log.info("返回结果:" + msgJson);
  34. return "success";
  35. } else {
  36. log.error("获取token失败:errcode = " + errcode);
  37. }
  38. }
  39. } catch (IOException e) {
  40. e.printStackTrace();
  41. }
  42. return "error";
  43. }
  44. /**
  45. * 获取设备票据
  46. */
  47. public static AjaxResult getSnTicket(String modelId, String sn) {
  48. String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=?&secret=?";
  49. String access_token = "";
  50. try (Response response = OkHttpUtil.get(url)) {
  51. log.info("接口调用凭证:response=" + response.toString());
  52. if (response.isSuccessful()) {
  53. JSONObject resData = JSON.parseObject(response.body().string());
  54. Integer errcode = resData.getInteger("errcode");
  55. if (errcode == null || errcode == 0) {
  56. access_token = resData.getString("access_token");
  57. String sendUrl = "https://api.weixin.qq.com/wxa/getsnticket?access_token=" + access_token;
  58. JSONObject jsonObject = new JSONObject();
  59. jsonObject.put("sn", sn);
  60. jsonObject.put("model_id", modelId);
  61. Response msgResponse = OkHttpUtil.postJson(sendUrl, null, jsonObject.toJSONString());
  62. JSONObject msgJson = JSONObject.parseObject(msgResponse.body().string());
  63. if (msgJson.getString("errcode").equals("0")) {
  64. return AjaxResult.success(msgJson.getString("sn_ticket"));
  65. }
  66. return AjaxResult.error();
  67. }
  68. }
  69. } catch (Exception ignored) {
  70. }
  71. return AjaxResult.error();
  72. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/674791
推荐阅读
相关标签
  

闽ICP备14008679号