SpringBoot小程序推送信息怎么实现(springboot,小程序,开发技术)

时间:2024-05-05 14:54:25 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

1.小程序推送信息列如我们去餐厅等位有预约提醒,剩余桌数

首先申请一个小程序,微信开放平台:小程序

2.申请小程序信息,申请信息模板

appid

AppSecret

SpringBoot小程序推送信息怎么实现

3.根据开发文档开发

subscribeMessage.send | 微信开放文档

4.代码如下:

引入依赖

<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.2</version></dependency>

先准备一个HTTP工具类Z

importorg.apache.http.HttpEntity;importorg.apache.http.client.methods.CloseableHttpResponse;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.conn.ssl.NoopHostnameVerifier;importorg.apache.http.conn.ssl.SSLConnectionSocketFactory;importorg.apache.http.conn.ssl.TrustSelfSignedStrategy;importorg.apache.http.entity.StringEntity;importorg.apache.http.impl.client.CloseableHttpClient;importorg.apache.http.impl.client.HttpClients;importorg.apache.http.ssl.SSLContexts;importorg.apache.http.util.EntityUtils;importjava.io.IOException;/***@authorlrx*@description:TODO*@date2021/3/99:50*/publicclassHttpClientUtils{//Http协议GET请求publicstaticStringhttpGet(Stringurl)throwsIOException{//初始化HttpClientCloseableHttpClienthttpClient=HttpClients.createDefault();//创建HttpGetHttpGethttpGet=newHttpGet(url);//发起请求,获取response对象CloseableHttpResponseresponse=httpClient.execute(httpGet);//获取请求状态码//response.getStatusLine().getStatusCode();//获取返回数据实体对象HttpEntityentity=response.getEntity();//转为字符串Stringresult=EntityUtils.toString(entity,"UTF-8");returnresult;}//Http协议Post请求publicstaticStringhttpPost(Stringurl,Stringjson)throwsException{//初始HttpClientCloseableHttpClienthttpClient=HttpClients.createDefault();//创建Post对象HttpPosthttpPost=newHttpPost(url);//设置Content-Type/*httpPost.setHeader("Content-Type","application/x-www-form-urlencoded");*/StringEntityse=newStringEntity(json,"UTF-8");se.setContentType("application/x-www-form-urlencoded");httpPost.setEntity(se);//发起请求,获取response对象CloseableHttpResponseresponse=httpClient.execute(httpPost);//获取请求码//response.getStatusLine().getStatusCode();//获取返回数据实体对象HttpEntityentity=response.getEntity();//转为字符串Stringresult=EntityUtils.toString(entity,"UTF-8");returnresult;}//Https协议Get请求publicstaticStringhttpsGet(Stringurl)throwsException{CloseableHttpClienthp=createSSLClientDefault();HttpGethg=newHttpGet(url);CloseableHttpResponseresponse=hp.execute(hg);HttpEntityentity=response.getEntity();Stringcontent=EntityUtils.toString(entity,"UTF-8");hp.close();returncontent;}//Https协议Post请求publicstaticStringhttpsPost(Stringurl,Stringjson)throwsException{CloseableHttpClienthp=createSSLClientDefault();HttpPosthttpPost=newHttpPost(url);httpPost.setHeader("Content-Type","application/json");httpPost.setEntity(newStringEntity(json));CloseableHttpResponseresponse=hp.execute(httpPost);HttpEntityentity=response.getEntity();Stringcontent=EntityUtils.toString(entity,"UTF-8");hp.close();returncontent;}publicstaticCloseableHttpClientcreateSSLClientDefault()throwsException{//如果下面的方法证书还是不过,报错的话试试下面第二种/*SSLContextsslContext=newSSLContextBuilder().loadTrustMaterial(null,newTrustStrategy(){//信任所有publicbooleanisTrusted(X509Certificate[]chain,StringauthType)throwsCertificateException{returntrue;}}).build();SSLConnectionSocketFactorysslsf=newSSLConnectionSocketFactory(sslContext);returnHttpClients.custom().setSSLSocketFactory(sslsf).build();*/SSLConnectionSocketFactorysslsf=newSSLConnectionSocketFactory(SSLContexts.custom().loadTrustMaterial(null,newTrustSelfSignedStrategy()).build(),NoopHostnameVerifier.INSTANCE);returnHttpClients.custom().setSSLSocketFactory(sslsf).build();}}

测试代码

importcom.alibaba.fastjson.JSONObject;importjava.io.IOException;importjava.util.Date;importjava.util.HashMap;importjava.util.Map;/***@authorlrx*@description:TODO小程序推送*@date2022/4/1113:32*/publicclassTestXCXMain{publicstaticvoidmain(String[]args)throwsException{Stringappid="";//appidStringsecret="";//secret//登录链接获取tokenStringloginUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret;Map<String,Object>payloadMap=JSONObject.parseObject(HttpClientUtils.httpGet(loginUrl));Stringtoken=null;if(payloadMap.containsKey("access_token")){token=payloadMap.get("access_token").toString();}System.out.println("获取token"+token);Map<String,Object>qqMap=newHashMap<String,Object>();qqMap.put("touser","openid");//要推送的openidqqMap.put("template_id","");//信息模板idqqMap.put("page","index");qqMap.put("miniprogram_state","developer");qqMap.put("lang","zh_CN");//封装信息Map<String,Object>dataMap=newHashMap<String,Object>();Map<String,String>valueMap1=newHashMap<String,String>();valueMap1.put("value","成功");Map<String,String>valueMap2=newHashMap<String,String>();valueMap2.put("value","成功");dataMap.put("thing3",valueMap1);dataMap.put("thing1",valueMap2);qqMap.put("data",dataMap);//发送Map<String,Object>payloadMapData=JSONObject.parseObject(HttpClientUtils.httpPost("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+token,JSONObject.toJSONString(qqMap)));if(payloadMapData.containsKey("errCode")){System.out.println("返回code码:"+payloadMapData.get("errCode").toString());}}}

5.推送结果

SpringBoot小程序推送信息怎么实现

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:SpringBoot小程序推送信息怎么实现的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Element Plus的el-icon如何用下一篇:

10 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18