微信小程序订阅消息推送(附带后台java代码)
官方文档: 小程序前端: 小程序服务端:
一,代码实现(后端)
①定义实体类
package com.saic.fin.system.sendInfo;
import lombok.Data;
import java.util.Map;
@Data
public class WxMsgVo {
    private String touser;//用户openId
    private String template_id;//模版id
    private Map<String , TemplateData> data;//推送文字
    private String page="pages/index/index";//跳转路径 ,默认跳转到小程序首页
} 
package com.saic.fin.system.sendInfo;
public class TemplateData {
    private String value;
    public TemplateData(String value) {
        this.value = value;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
} 
②定义HttpUtil类(Get请求)
package com.saic.fin.system.util;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpUtil {
    public static final String APPID = "";//小程序APPID(小程序ID)
    public static final String APPSECRET = "";//小程序APPSECRET(小程序秘钥)
    public static JSONObject doGetJson(String URL) throws IOException {
        JSONObject jsonObject = null;
        HttpURLConnection conn = null;
        InputStream is = null;
        BufferedReader br = null;
        StringBuilder result = new StringBuilder();
        try {
            //创建远程url连接对象
            URL url = new URL(URL);
            //通过远程url连接对象打开一个连接,强转成HTTPURLConnection类
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            //设置连接超时时间和读取超时时间
            conn.setConnectTimeout(15000);
            conn.setReadTimeout(60000);
            conn.setRequestProperty("Accept", "application/json");
            //发送请求
            conn.connect();
            //通过conn取得输入流,并使用Reader读取
            if (200 == conn.getResponseCode()) {
                is = conn.getInputStream();
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                String line;
                while ((line = br.readLine()) != null) {
                    result.append(line);
                    System.out.println(line);
                }
            } else {
                System.out.println("ResponseCode is an error code:" + conn.getResponseCode());
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (is != null) {
                    is.close();
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
            conn.disconnect();
        }
        jsonObject = JSONObject.parseObject(result.toString());
        return jsonObject;
    }
} 
③验证token过期时间
④小程序发送消息
AuthUtil工具类
上一篇:
			            uniapp开发微信小程序-2.页面制作 
			          
			          
			        