java订阅_⼩程序订阅消息(java后端实现)开发
订阅消息说明
订阅消息是近期新出的⼀个能⼒,⽤来代替原有的模板消息(原有的模板消息即将下线)
订阅消息的功能较模板消息有所提升,"7天"的限制取消,同时有"⼀次性"和"永久"订阅.(功能上是这样说的,但是实际开发时发现"永久"订阅还是对⼩程序的服务类⽬有要求的,客户的⼩程序只⽀持"⼀次性"订阅)
官⽅通道:
⼩程序前端:点击进⼊
⼩程序服务端:点击进⼊
开发思路
⽤户在⼩程序内触发按钮或进⾏⽀付操作时前端调⽤订阅消息授权框,默认⼀次授权只能发送⼀次订阅消息
如果⽤户勾选"下次⾃动授权",下次将不再弹出授权框->点击按钮直接拥有⼀次发送订阅消息的机会,此处不需要模板消息的"formId",较之前更简单
经过测试,如果在⼩程序上多次点击触发授权的按钮,发送订阅消息的机会可以累加!!!
(如,1分钟内点击了10次按钮,后⾯将拥有10次发送订阅消息的机会,什么时候发都可以)
代码实现(仅java后端)
实体类部分
1.TemplateParam.java
public class TemplateParam {
private String key;
private String value;
public TemplateParam(String key,String value){
this.key=key;
this.value=value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}
2.Template.java
import java.util.List;
有趣的java小程序public class Template {
private String touser;
private String template_id;
private String page;
private List templateParamList;
public String getTouser() {
return touser;
}
public void setTouser(String touser) {
}
public String getTemplate_id() {
return template_id;
}
public void setTemplate_id(String template_id) {
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public String toJSON() {
StringBuffer buffer = new StringBuffer();
buffer.append("{");
buffer.append(String.format("\"touser\":\"%s\"", user)).append(",");
buffer.append(String.format("\"template_id\":\"%s\"", plate_id)).append(","); buffer.append(String.format("\"page\":\"%s\"", this.page)).append(",");
buffer.append("\"data\":{");
TemplateParam param = null;
for (int i = 0; i < plateParamList.size(); i++) {
param = (i);
// 判断是否追加逗号
if (i < plateParamList.size() - 1){
buffer.append(String.format("\"%s\": {\"value\":\"%s\"},", Key(), Value())); }else{
buffer.append(String.format("\"%s\": {\"value\":\"%s\"}", Key(), Value())); }
}
buffer.append("}");
buffer.append("}");
String();
}
public List getTemplateParamList() {
return templateParamList;
}
public void setTemplateParamList(List templateParamList) {
}
}
⼯具类部分
1.CommonUtil.java
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.ConnectException;
import java.HttpURLConnection;
import java.URL;
import javax.ssl.HttpsURLConnection;
import javax.ssl.SSLContext;
import javax.ssl.SSLSocketFactory;
import javax.ssl.TrustManager;
import net.sf.json.JSONObject;
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 = Instance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = SocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
httpUrlConn.setSSLSocketFactory(ssf);
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
// 设置请求⽅式(GET/POST)
httpUrlConn.setRequestMethod(requestMethod);
if ("GET".equalsIgnoreCase(requestMethod)) {
}
// 当有数据需要提交时
if (null != outputStr) {
OutputStream outputStream = OutputStream();
// 注意编码格式,防⽌中⽂乱码
outputStream.Bytes("UTF-8"));
outputStream.close();
}
// 将返回的输⼊流转换成字符串
InputStream inputStream = InputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = adLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
jsonObject = JSONObject.String());
} catch (ConnectException ce) {
ce.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
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) url.openConnection(); httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
// 设置请求⽅式(GET/POST)
httpUrlConn.setRequestMethod(requestMethod);
if ("GET".equalsIgnoreCase(requestMethod)) {
}
// 当有数据需要提交时

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。