springboot小程序推送信息的项目实践-亚博电竞手机版

目录

  • 1.小程序推送信息列如我们去餐厅等位有预约提醒,剩余桌数
  • 2.申请小程序信息,申请信息模板
  • 3.根据开发文档开发
  • 4.代码如下:
  • 5.推送结果

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

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

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

appid

appsecret

3.根据开发文档开发

subscribemessage.send | 微信开放文档

4.代码如下:

引入依赖

org.apache.httpcomponents httpclient 4.5.2

先准备一个http工具类z

import org.apache.http.httpentity; import org.apache.http.client.methods.closeablehttpresponse; import org.apache.http.client.methods.httpget; import org.apache.http.client.methods.httppost; import org.apache.http.conn.ssl.noophostnameverifier; import org.apache.http.conn.ssl.sslconnectionsocketfactory; import org.apache.http.conn.ssl.trustselfsignedstrategy; import org.apache.http.entity.stringentity; import org.apache.http.impl.client.closeablehttpclient; import org.apache.http.impl.client.httpclients; import org.apache.http.ssl.sslcontexts; import org.apache.http.util.entityutils; import java.io.ioexception; /** * @author lrx * @description: todo * @date 2021/3/9 9:50 */ public class httpclientutils { //http协议get请求 public static string httpget(string url) throws ioexception { //初始化httpclient closeablehttpclient httpclient = httpclients.createdefault(); //创建httpget httpget httpget = new httpget(url); //发起请求,获取response对象 closeablehttpresponse response = httpclient.execute(httpget); //获取请求状态码 //response.getstatusline().getstatuscode(); //获取返回数据实体对象 httpentity entity = response.getentity(); //转为字符串 string result = entityutils.tostring(entity, "utf-8"); return result; } //http协议post请求 public static string httppost(string url, string json) throws exception { //初始httpclient closeablehttpclient httpclient = httpclients.createdefault(); //创建post对象 httppost httppost = new httppost(url); //设置content-type /* httppost.setheader("content-type", "application/x-www-form-urlencoded");*/ stringentity se = new stringentity(json,"utf-8"); se.setcontenttype(odo 小程序推送 * @date 2022/4/11 13:32 */ public class testxcxmain { public static void main(string[] args) throws exception { string appid = ""; //appid string secret = ""恰卡编程网; //secret //登录链接获取token string loginurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" appid "&secret=" secret; map payloadmap = jsonobject.parseobject(httpclientutils.httpget(loginurl)); string token = null; if (payloadmap.containskey("access_token")) { token = payloadmap.get("access_token").tostring(); } system.out.println("获取token" token); map qqmap = new hashmap(); qqmap.put("touser", "openid"); //要推送的openid qqmaphttp://www.cppcns.com.put("template_id", ""); //信息模板id qqmap.put("page", "index"); qqmap.put("miniprogram_state", "developer"); qqmap.put("lang", "zh_cn"); //封装信息 map datamap = new hashmap(); map valuemap1 = new hashmap(); valuemap1.put("value", "成功"); map valuemap2 = new hashmap(); valuemap2.put("value", "成功"); datamap.put("thing3", valuemap1); datamap.put("thing1", valuemap2); qqmap.put("data", datamap); //发送 map 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小程序推送信息的项目实践的文章就介绍到这了,更多相关springboot小程序推送信息内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

展开全文
内容来源于互联网和用户投稿,文章中一旦含有亚博电竞手机版的联系方式务必识别真假,本站仅做信息展示不承担任何相关责任,如有侵权或涉及法律问题请联系亚博电竞手机版删除

最新文章

网站地图