当前位置:   article > 正文

Java实现微信小程序发送订阅消息_java发送微信小程序订阅消息

java发送微信小程序订阅消息

一、创建订阅消息模板

        1.登入微信公众平台: https://mp.weixin.qq.com/

        2.创建消息模板

        

        勾选右侧需要的关键字

        

     

 

二、代码实现

     1.小程序代码

      错误:errcode":43101 需要小程序允许接收通知才行,允许一次可发送一次,允许可叠加

  1. //tmplIds模板id(一次订阅可能有多个id','隔开)
  2. wx.requestSubscribeMessage({
  3. tmplIds: [''],
  4. success (res) { }
  5. })

    2.java 后端代码

        2.1 获取 AccessToken

        请求url:https://api.weixin.qq.com/cgi-bin/token

        官方文档: auth.getAccessToken | 微信开放文档 (qq.com) 

        doGet是我自己封装的请求

 

  1. public String getAccessToken(){
  2. String appid="";//小程序appid
  3. String appSecret="";//小程序密钥
  4. String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+appSecret;
  5. String token="";
  6. try {
  7. token= doGetPost(url,"GET",null);
  8. //处理返回的值
  9. JSONObject tokenJson = JSONObject.parseObject(token);
  10. token = tokenJson.get("access_token").toString();
  11. } catch (Exception e) {
  12. e.printStackTrace();
  13. }
  14. return token;
  15. }

     2.2 发送消息模板

        请求url:https://api.weixin.qq.com/cgi-bin/message/subscribe/send

        官方文档:subscribeMessage.send | 微信开放文档 (qq.com)

        我没有封装消息模板的详细内容参数,就直接json.parse

        模板文字描述参数根据模板详情的详细内容来改以此类推

        

  1. public Object text(){
  2. String accessToken=getAccessToken();//获取accessToken
  3. //模板文字描述参数
  4. Object data="{\"thing1\":{\"value\":\"日常保洁\"},\"thing2\":{\"value\":\"小红\"},\"phone_number3\":{\"value\":\"12345678900\"} }";
  5. //请求路径
  6. String url="https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+accessToken;
  7. //参数
  8. HashMap<String, Object> paramMap = new HashMap<String, Object>();
  9. paramMap.put("touser","");//接收者openid
  10. paramMap.put("template_id","");//所需下发的订阅模板id
  11. paramMap.put("page","pages/index/index");//点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,
  12. paramMap.put("data",JSON.parse(data.toString()));//模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }
  13. Object o= null;
  14. try {
  15. o = doGetPost(url,"POST",paramMap);
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. return o;
  20. }

 

  1. public String doGetPost(String apiUrl,String methodType,Map<String,Object> map){
  2. OutputStreamWriter outputStreamWriter = null;
  3. InputStream inputStream = null;
  4. String result = null;
  5. try{
  6. URL url = new URL(apiUrl);// 创建连接
  7. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  8. httpURLConnection.setDoOutput(true);
  9. httpURLConnection.setDoInput(true);
  10. httpURLConnection.setUseCaches(false);
  11. httpURLConnection.setInstanceFollowRedirects(true);
  12. httpURLConnection.setRequestMethod(methodType) ;
  13. httpURLConnection.setRequestProperty("Accept", "application/json");
  14. httpURLConnection.setRequestProperty("Content-Type", "application/json");
  15. httpURLConnection.connect();
  16. if(methodType.equals("POST")){
  17. outputStreamWriter = new OutputStreamWriter(httpURLConnection.getOutputStream(), "UTF-8");
  18. outputStreamWriter.append(JSON.toJSONString(map));
  19. outputStreamWriter.flush();
  20. outputStreamWriter.close();
  21. }
  22. // 读取响应
  23. inputStream = httpURLConnection.getInputStream();
  24. int length = (int) httpURLConnection.getContentLength();
  25. if (length != -1) {
  26. byte[] data = new byte[length];
  27. byte[] t = new byte[512];
  28. int len = 0;
  29. int pos = 0;
  30. while ((len = inputStream.read(t)) > 0) {
  31. System.arraycopy(t, 0, data, pos, len);
  32. pos += len;
  33. }
  34. result = new String(data, "UTF-8");
  35. }
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. } finally {
  39. try {
  40. inputStream.close();
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. return result;
  46. }

 

 

 

 

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

闽ICP备14008679号