2023年8月3日发(作者:)
微信⼩程序订阅消息(java后端实现)开发订阅消息说明订阅消息是微信近期新出的⼀个能⼒,⽤来代替原有的模板消息(原有的模板消息即将下线)订阅消息的功能较模板消息有所提升,"7天"的限制取消,同时有"⼀次性"和"永久"订阅.(功能上是这样说的,但是实际开发时发现"永久"订阅还是对⼩程序的服务类⽬有要求的,客户的⼩程序只⽀持"⼀次性"订阅)官⽅通道:⼩程序前端:⼩程序服务端:开发思路⽤户在⼩程序内触发按钮或进⾏⽀付操作时前端调⽤订阅消息授权框,默认⼀次授权只能发送⼀次订阅消息如果⽤户勾选"下次⾃动授权",下次将不再弹出授权框->点击按钮直接拥有⼀次发送订阅消息的机会,此处不需要模板消息的"formId",较之前更简单经过测试,如果在⼩程序上多次点击触发授权的按钮,发送订阅消息的机会可以累加(如,1分钟内点击了10次按钮,后⾯将拥有10次发送订阅消息的机会,什么时候发都可以)代码实现(仅java后端)实体类部分blic class TemplateParam {
private String key;
private String value;
public TemplateParam(String key,String value){
=key;
=value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
= value;
}public String getKey() { return key;}public void setKey(String key) { = key;}
}port ;public class Template {
private String touser;
private String template_id;
private String page;private List
public String getTouser() { return touser;}public void setTouser(String touser) { = touser;}public String getTemplate_id() { return template_id;}public void setTemplate_id(String template_id) { te_id = template_id;}public String getPage() { return page;}public void setPage(String page) { = page;}public String toJSON() {
StringBuffer buffer = new StringBuffer();
("{");
((""touser":"%s"", )).append(",");
((""template_id":"%s"", te_id)).append(",");
((""page":"%s"", )).append(",");
(""data":{");
TemplateParam param = null;
for (int i = 0; i < (); i++) {
param = (i);
// 判断是否追加逗号
if (i < () - 1){
((""%s": {"value":"%s"},", (), ue()));
}else{
((""%s": {"value":"%s"}", (), ue()));
}
}
("}");
("}");
return ng();
}
public List
return templateParamList;
}
public void setTemplateParamList(List
teParamList = templateParamList;
}
}⼯具类部分port edReader;
import tream;
import treamReader;
import Stream;
import ortedEncodingException;
import tException;
import LConnection;
import ;
import RLConnection;
import text;
import ketFactory;
import anager;
import ject;
public class CommonUtil {
public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) {
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
try { // 创建SSLContext对象,并使⽤我们指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = tance("SSL", "SunJSSE");
(null, tm, new Random());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = ketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection httpUrlConn = (HttpsURLConnection) nnection();
SocketFactory(ssf);
utput(true);
nput(true);
Caches(false);
// 设置请求⽅式(GET/POST)
uestMethod(requestMethod);
if ("GET".equalsIgnoreCase(requestMethod)) {
t();
}
// 当有数据需要提交时
if (null != outputStr) {
OutputStream outputStream = putStream();
// 注意编码格式,防⽌中⽂乱码
(es("UTF-8"));
();
}
// 将返回的输⼊流转换成字符串
InputStream inputStream = utStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = ne()) != null) {
(str);
}
();
();
// 释放资源
();
inputStream = null;
nect();
jsonObject = ject(ng());
} catch (ConnectException ce) {
tackTrace();
} catch (Exception e) {
tackTrace();
}
return jsonObject;
}
public static String httpRequest(String requestUrl, String requestMethod, String outputStr) {
StringBuffer buffer = new StringBuffer();
try {
URL url = new URL(requestUrl);
HttpURLConnection httpUrlConn = (HttpURLConnection) nnection();
utput(true);
nput(true);
Caches(false);
// 设置请求⽅式(GET/POST)
uestMethod(requestMethod);
if ("GET".equalsIgnoreCase(requestMethod)) {
t();
}
// 当有数据需要提交时
if (null != outputStr) {
OutputStream outputStream = putStream();
// 注意编码格式,防⽌中⽂乱码
(es("UTF-8"));
();
}
// 将返回的输⼊流转换成字符串
InputStream inputStream = utStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = ne()) != null) {
(str);
}
();
();
// 释放资源
();
inputStream = null;
nect();
//jsonObject = ject(ng());
} catch (ConnectException ce) {
tackTrace();
} catch (Exception e) {
tackTrace();
}
return ng();
}
public static String urlEncodeUTF8(String source){
String result = source;
try {
result = (source,"utf-8");
} catch (UnsupportedEncodingException e) {
tackTrace();
}
return result;
}
public static String httpsRequestForStr(String requestUrl, String requestMethod, String outputStr) {
String result="";
StringBuffer buffer = new StringBuffer();
try {
// 创建SSLContext对象,并使⽤我们指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = tance("SSL", "SunJSSE");
(null, tm, new Random());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = ketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection httpUrlConn = (HttpsURLConnection) nnection();
SocketFactory(ssf);
utput(true);
nput(true);
Caches(false);
// 设置请求⽅式(GET/POST)
uestMethod(requestMethod);
if ("GET".equalsIgnoreCase(requestMethod)) {
t();
}
// 当有数据需要提交时
if (null != outputStr) {
OutputStream outputStream = putStream();
// 注意编码格式,防⽌中⽂乱码
(es("UTF-8"));
();
} // 将返回的输⼊流转换成字符串
InputStream inputStream = utStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = ne()) != null) {
(str);
}
();
();
// 释放资源
();
inputStream = null;
nect();
result=ng();
} catch (ConnectException ce) {
tackTrace();
} catch (Exception e) {
tackTrace();
}
return result;
}
}port ption;import ist;import ;import ;import ;import tity;import luePair;import odedFormEntity;import bleHttpResponse;import t;import st;import bleHttpClient;import ients;import ameValuePair;import Utils;public class HttpUtil {private static final CloseableHttpClient httpclient = Default();/** * 发送HttpGet请求 * @param url * @return */public static String sendGet(String url) { HttpGet httpget = new HttpGet(url); CloseableHttpResponse response = null; try { response = e(httpget); } catch (IOException e1) { tackTrace(); } String result = null; try { HttpEntity entity = ity(); if (entity != null) { result = ng(entity); } } catch (Exception e) { tackTrace(); } finally { try { (); } catch (IOException e) { tackTrace(); } } return result;}/** * 发送HttpPost请求,参数为map * @param url * @param map * @return */public static String sendPost(String url, Map
}jar包:控制层代码:1.获取ACCESS_TOKENString url="/cgi-bin/token?grant_type=client_credential&appid=" + ⼩程序的appid + "&secret=" + ⼩程序的Secret String result = t(url); JSONObject object=bject(result); String Access_Token = ing("access_token");2.发送订阅消息 Template template=new Template();
plate_id("填写⼩程序申请的订阅消息id");
ser("⽤户的openid"); e("pages/index/index"); List
(new TemplateParam("character_string2","000001"));
(new TemplateParam("amount1","888.88"));
(new TemplateParam("date3","2015年01⽉05⽇"));
(new TemplateParam("thing4","请进⼊⼩程序查1看"));
plateParamList(paras);
String requestUrl="/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN";
requestUrl=e("ACCESS_TOKEN", Access_Token);
n(()); ject jsonResult=equest(requestUrl, "POST", ());
if(jsonResult!=null){
n(jsonResult); int errorCode=("errcode");
String errorMessage=ing("errmsg");
if(errorCode==0){
n("Send Success"); }else{
n("订阅消息发送失败:"+errorCode+","+errorMessage);
}
}
总结1.本⽂阅读对象为初学者,所有各种⼯具类.jar包都粘出来了,直接复制即可使⽤2.通过该功能的开发,发现⼩程序的通知类功能监管更加严格,必须⽤户授权才可以发订阅消息,同时⽤户可以更⽅便的取消订阅,所以建议开发者慎⽤此功能到此这篇关于微信⼩程序订阅消息(java后端实现)开发的⽂章就介绍到这了,更多相关⼩程序订阅消息内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!
2023年8月3日发(作者:)
微信⼩程序订阅消息(java后端实现)开发订阅消息说明订阅消息是微信近期新出的⼀个能⼒,⽤来代替原有的模板消息(原有的模板消息即将下线)订阅消息的功能较模板消息有所提升,"7天"的限制取消,同时有"⼀次性"和"永久"订阅.(功能上是这样说的,但是实际开发时发现"永久"订阅还是对⼩程序的服务类⽬有要求的,客户的⼩程序只⽀持"⼀次性"订阅)官⽅通道:⼩程序前端:⼩程序服务端:开发思路⽤户在⼩程序内触发按钮或进⾏⽀付操作时前端调⽤订阅消息授权框,默认⼀次授权只能发送⼀次订阅消息如果⽤户勾选"下次⾃动授权",下次将不再弹出授权框->点击按钮直接拥有⼀次发送订阅消息的机会,此处不需要模板消息的"formId",较之前更简单经过测试,如果在⼩程序上多次点击触发授权的按钮,发送订阅消息的机会可以累加(如,1分钟内点击了10次按钮,后⾯将拥有10次发送订阅消息的机会,什么时候发都可以)代码实现(仅java后端)实体类部分blic class TemplateParam {
private String key;
private String value;
public TemplateParam(String key,String value){
=key;
=value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
= value;
}public String getKey() { return key;}public void setKey(String key) { = key;}
}port ;public class Template {
private String touser;
private String template_id;
private String page;private List
public String getTouser() { return touser;}public void setTouser(String touser) { = touser;}public String getTemplate_id() { return template_id;}public void setTemplate_id(String template_id) { te_id = template_id;}public String getPage() { return page;}public void setPage(String page) { = page;}public String toJSON() {
StringBuffer buffer = new StringBuffer();
("{");
((""touser":"%s"", )).append(",");
((""template_id":"%s"", te_id)).append(",");
((""page":"%s"", )).append(",");
(""data":{");
TemplateParam param = null;
for (int i = 0; i < (); i++) {
param = (i);
// 判断是否追加逗号
if (i < () - 1){
((""%s": {"value":"%s"},", (), ue()));
}else{
((""%s": {"value":"%s"}", (), ue()));
}
}
("}");
("}");
return ng();
}
public List
return templateParamList;
}
public void setTemplateParamList(List
teParamList = templateParamList;
}
}⼯具类部分port edReader;
import tream;
import treamReader;
import Stream;
import ortedEncodingException;
import tException;
import LConnection;
import ;
import RLConnection;
import text;
import ketFactory;
import anager;
import ject;
public class CommonUtil {
public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) {
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
try { // 创建SSLContext对象,并使⽤我们指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = tance("SSL", "SunJSSE");
(null, tm, new Random());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = ketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection httpUrlConn = (HttpsURLConnection) nnection();
SocketFactory(ssf);
utput(true);
nput(true);
Caches(false);
// 设置请求⽅式(GET/POST)
uestMethod(requestMethod);
if ("GET".equalsIgnoreCase(requestMethod)) {
t();
}
// 当有数据需要提交时
if (null != outputStr) {
OutputStream outputStream = putStream();
// 注意编码格式,防⽌中⽂乱码
(es("UTF-8"));
();
}
// 将返回的输⼊流转换成字符串
InputStream inputStream = utStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = ne()) != null) {
(str);
}
();
();
// 释放资源
();
inputStream = null;
nect();
jsonObject = ject(ng());
} catch (ConnectException ce) {
tackTrace();
} catch (Exception e) {
tackTrace();
}
return jsonObject;
}
public static String httpRequest(String requestUrl, String requestMethod, String outputStr) {
StringBuffer buffer = new StringBuffer();
try {
URL url = new URL(requestUrl);
HttpURLConnection httpUrlConn = (HttpURLConnection) nnection();
utput(true);
nput(true);
Caches(false);
// 设置请求⽅式(GET/POST)
uestMethod(requestMethod);
if ("GET".equalsIgnoreCase(requestMethod)) {
t();
}
// 当有数据需要提交时
if (null != outputStr) {
OutputStream outputStream = putStream();
// 注意编码格式,防⽌中⽂乱码
(es("UTF-8"));
();
}
// 将返回的输⼊流转换成字符串
InputStream inputStream = utStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = ne()) != null) {
(str);
}
();
();
// 释放资源
();
inputStream = null;
nect();
//jsonObject = ject(ng());
} catch (ConnectException ce) {
tackTrace();
} catch (Exception e) {
tackTrace();
}
return ng();
}
public static String urlEncodeUTF8(String source){
String result = source;
try {
result = (source,"utf-8");
} catch (UnsupportedEncodingException e) {
tackTrace();
}
return result;
}
public static String httpsRequestForStr(String requestUrl, String requestMethod, String outputStr) {
String result="";
StringBuffer buffer = new StringBuffer();
try {
// 创建SSLContext对象,并使⽤我们指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = tance("SSL", "SunJSSE");
(null, tm, new Random());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = ketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection httpUrlConn = (HttpsURLConnection) nnection();
SocketFactory(ssf);
utput(true);
nput(true);
Caches(false);
// 设置请求⽅式(GET/POST)
uestMethod(requestMethod);
if ("GET".equalsIgnoreCase(requestMethod)) {
t();
}
// 当有数据需要提交时
if (null != outputStr) {
OutputStream outputStream = putStream();
// 注意编码格式,防⽌中⽂乱码
(es("UTF-8"));
();
} // 将返回的输⼊流转换成字符串
InputStream inputStream = utStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = ne()) != null) {
(str);
}
();
();
// 释放资源
();
inputStream = null;
nect();
result=ng();
} catch (ConnectException ce) {
tackTrace();
} catch (Exception e) {
tackTrace();
}
return result;
}
}port ption;import ist;import ;import ;import ;import tity;import luePair;import odedFormEntity;import bleHttpResponse;import t;import st;import bleHttpClient;import ients;import ameValuePair;import Utils;public class HttpUtil {private static final CloseableHttpClient httpclient = Default();/** * 发送HttpGet请求 * @param url * @return */public static String sendGet(String url) { HttpGet httpget = new HttpGet(url); CloseableHttpResponse response = null; try { response = e(httpget); } catch (IOException e1) { tackTrace(); } String result = null; try { HttpEntity entity = ity(); if (entity != null) { result = ng(entity); } } catch (Exception e) { tackTrace(); } finally { try { (); } catch (IOException e) { tackTrace(); } } return result;}/** * 发送HttpPost请求,参数为map * @param url * @param map * @return */public static String sendPost(String url, Map
}jar包:控制层代码:1.获取ACCESS_TOKENString url="/cgi-bin/token?grant_type=client_credential&appid=" + ⼩程序的appid + "&secret=" + ⼩程序的Secret String result = t(url); JSONObject object=bject(result); String Access_Token = ing("access_token");2.发送订阅消息 Template template=new Template();
plate_id("填写⼩程序申请的订阅消息id");
ser("⽤户的openid"); e("pages/index/index"); List
(new TemplateParam("character_string2","000001"));
(new TemplateParam("amount1","888.88"));
(new TemplateParam("date3","2015年01⽉05⽇"));
(new TemplateParam("thing4","请进⼊⼩程序查1看"));
plateParamList(paras);
String requestUrl="/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN";
requestUrl=e("ACCESS_TOKEN", Access_Token);
n(()); ject jsonResult=equest(requestUrl, "POST", ());
if(jsonResult!=null){
n(jsonResult); int errorCode=("errcode");
String errorMessage=ing("errmsg");
if(errorCode==0){
n("Send Success"); }else{
n("订阅消息发送失败:"+errorCode+","+errorMessage);
}
}
总结1.本⽂阅读对象为初学者,所有各种⼯具类.jar包都粘出来了,直接复制即可使⽤2.通过该功能的开发,发现⼩程序的通知类功能监管更加严格,必须⽤户授权才可以发订阅消息,同时⽤户可以更⽅便的取消订阅,所以建议开发者慎⽤此功能到此这篇关于微信⼩程序订阅消息(java后端实现)开发的⽂章就介绍到这了,更多相关⼩程序订阅消息内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!
发布评论