微信订阅号 -发送订阅消息-Java实现

微信开发者官方链接:发送订阅发送订阅消息 | 微信开放文档发送订阅

效果图

发送订阅消息工具类

package com.xxx.commons.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @BelongsProject: xxx
 * @BelongsPackage: com.xxx.commons.utils
 * @ClassName WxMaSubscribeMessageUtil
 * @Author: handsome boy__LiuWenCheng
 * @CreateTime: 2024-09-05  15:58
 */

@Component
public class WxMaSubscribeMessageUtil {
    public static String appId = "小程序id";
    public static String appSecret = "小程序秘钥";


    /**
     * 活动即将开始通知
     * @param openId
     * @param templateId
     * @param skip
     */
    public static void kaiSi(String openId, String templateId, String skip,LocalDateTime now,String t1 ,String t3,String t4) {
        String accessToken = getAccessToken();
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;
        JSONObject data = new JSONObject();
        data.put("touser", openId);
        data.put("template_id", templateId);
        data.put("page", "pages/activityDetail/activityDetail?activityId=" + skip);

        // 设置消息内容
        JSONObject messageData = new JSONObject();


        JSONObject thing1 = new JSONObject();
        thing1.put("value", t1);
        messageData.put("thing1", thing1);

        // 转化成年月日时分的时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        String formattedDateTime = now.format(formatter);

        JSONObject time2 = new JSONObject();
        time2.put("value", formattedDateTime);
        messageData.put("time2", time2);


        JSONObject thing3 = new JSONObject();
        thing3.put("value", t3);
        messageData.put("thing3", thing3);


        JSONObject thing4 = new JSONObject();
        //如果长度大于截取前20个字符
        if (t4.length() > 20) {
            thing4.put("value", t4.substring(0, 10) + "...");
        } else {
            thing4.put("value", t4);
        }
        messageData.put("thing4", thing4);


        // 打印 messageData 的内容
        System.out.println("messageData: " + messageData.toJSONString());

        data.put("data", messageData);
        // 打印 data 的内容
        System.out.println("data: " + data.toJSONString());

        // 发送消息
        String result = HttpUtil.post(url, data.toString());
        System.out.println("订阅消息结果:" + result);
    }




    public static String getAccessToken() {


        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
        url = url.replace("APPID", appId).replace("APPSECRET", appSecret);
        try {

            String result = HttpUtil.get(url);
            JSONObject jsonObject = JSON.parseObject(result);
            return jsonObject.getString("access_token");
        } catch (Exception e) {
            System.err.println("请求失败:" + e.getMessage());
            e.printStackTrace();
            return null;
        }

    }


}

测试

@SpringBootTest
public class OpenidTest {


    @Test
    public void testSubscribeMessage() {
        String openid = "用户openid";
        String templateId = "模版id";

        String skip = 5084+"";
        String t1 = "活动报名提醒";
        LocalDateTime now = LocalDateTime.now();
        String t3 = "活动即将开启,期待与您共同开启全新征途。";
        String t4 = "活动地址";
        WxMaSubscribeMessageUtil.kaiSi(openid, templateId, skip, now,t1, t3, t4);

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值