900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > Java实现微信小程序发送订阅消息

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

时间:2023-05-03 16:39:22

相关推荐

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

一、创建订阅消息模板

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

2.创建消息模板

勾选右侧需要的关键字

二、代码实现

1.小程序代码

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

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

2.java 后端代码

2.1 获取 AccessToken

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

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

doGet是我自己封装的请求

public String getAccessToken(){String appid="";//小程序appidString appSecret="";//小程序密钥String url="https://api./cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+appSecret;String token="";try {token= doGetPost(url,"GET",null);//处理返回的值JSONObject tokenJson = JSONObject.parseObject(token);token = tokenJson.get("access_token").toString();} catch (Exception e) {e.printStackTrace();}return token;}

2.2 发送消息模板

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

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

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

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

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

public String doGetPost(String apiUrl,String methodType,Map<String,Object> map){OutputStreamWriter outputStreamWriter = null;InputStream inputStream = null;String result = null;try{URL url = new URL(apiUrl);// 创建连接HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();httpURLConnection.setDoOutput(true);httpURLConnection.setDoInput(true);httpURLConnection.setUseCaches(false);httpURLConnection.setInstanceFollowRedirects(true);httpURLConnection.setRequestMethod(methodType) ;httpURLConnection.setRequestProperty("Accept", "application/json");httpURLConnection.setRequestProperty("Content-Type", "application/json"); httpURLConnection.connect();if(methodType.equals("POST")){outputStreamWriter = new OutputStreamWriter(httpURLConnection.getOutputStream(), "UTF-8"); outputStreamWriter.append(JSON.toJSONString(map));outputStreamWriter.flush();outputStreamWriter.close();}// 读取响应inputStream = httpURLConnection.getInputStream();int length = (int) httpURLConnection.getContentLength();if (length != -1) {byte[] data = new byte[length];byte[] t = new byte[512];int len = 0;int pos = 0;while ((len = inputStream.read(t)) > 0) {System.arraycopy(t, 0, data, pos, len);pos += len;}result = new String(data, "UTF-8");}} catch (IOException e) {e.printStackTrace();} finally {try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}return result;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。