使用java实现小程序发送订阅消息

需要导入的包

<dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.9.1</version>
        </dependency>


  <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.15</version>
        </dependency>

下面是实现代码

import com.alibaba.fastjson.JSONObject;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class SubscribeMessageDemo {
    //修改为你的小程序对应的相关信息
    private final static String APPID = "your_appid";
    private final static String APP_SECRET = "your_appsecret";
    //请求access_token的接口地址
    private final static String ACCESS_TOKEN_API = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+APPID+"&secret="+APP_SECRET;
    //发送订阅消息的接口地址
    private final static String SEND_SUBSCRIBE_MESSAGE_API = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=";

    public static void main(String[] args) {
        OkHttpClient client = new OkHttpClient();
        try {
            //1.获取access_token
            String accessToken = getAccessToken(client);

            //2.发送订阅消息
            String result = sendSubscribeMessage(client, accessToken);
            System.out.println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //获取access_token
    private static String getAccessToken(OkHttpClient client) throws IOException {
        Request request = new Request.Builder()
                .url(ACCESS_TOKEN_API)
                .build();
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            JSONObject jsonObject = JSONObject.parseObject(response.body().string());
            String accessToken = jsonObject.getString("access_token");
            return accessToken;
        }
        return null;
    }
    //发送订阅消息
    private static String sendSubscribeMessage(OkHttpClient client, String accessToken) throws IOException {
        String url = SEND_SUBSCRIBE_MESSAGE_API + accessToken;

        //构造发送给用户的订阅消息内容
        Map<String, Object> messageContent = new HashMap<>();
        messageContent.put("thing1", new HashMap<String, Object>(){
   
   {put("value", "预定成功");}});
        messageContent.put("thing2", new HashMap<String, Object>(){
   
   {put("value", "2023-04-04 16:29:49");}});
        JSONObject messageContentJson = new JSONObject(messageContent);

        //构造订阅消息
        Map<String, Object> subscribeMessage = new HashMap<>();
        subscribeMessage.put("touser", "openid");//填写你的接收者openid
        subscribeMessage.put("template_id", "your_template_id");//填写你的模板ID
        subscribeMessage.put("data", messageContentJson);
        JSONObject subscribeMessageJson = new JSONObject(subscribeMessage);

        RequestBody requestBody = RequestBody.create(subscribeMessageJson.toJSONString(), okhttp3.MediaType.parse("application/json; charset=utf-8"));

        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            return response.body().string();
        }
        return null;
    }
}

 json参数的格式请参考微信官方文档发送订阅消息 | 微信开放文档

猜你喜欢

转载自blog.csdn.net/ImisLi/article/details/129958653