微信小程序消息推送

说明:

通过引入weixin-java-miniapp SDK快速开发,实现小程序推送消息功能。

引入pom.xml依赖

<dependency>
    <groupId>com.github.binarywang</groupId>
    <artifactId>weixin-java-miniapp</artifactId>
    <version>4.4.0</version>
</dependency>

源码

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaSubscribeService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *  微信通知
 */
@Slf4j
@Service
public class WeChatNotifyService {

    @Autowired
    private WxMaService wxMaService;

    /**
     * 发送微信通知
     * @param openId
     * @param templateId
     * @param page
     * @param data
     * @return
     */
    public Boolean sendMiniAppNotification(String openId, String templateId, String page, Map<String,String> data) {
        // 转换为List对象
        List<WxMaSubscribeMessage.MsgData> list = new ArrayList<>();
        for (Map.Entry<String, String> entry : data.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            WxMaSubscribeMessage.MsgData obj = new WxMaSubscribeMessage.MsgData(key, value);
            list.add(obj);
        }
        WxMaSubscribeMessage templateMessage = WxMaSubscribeMessage.builder()
                .toUser(openId)
                .templateId(templateId)
                .page(page)
                .data(list)
                .build();

        try {
            WxMaSubscribeService subscribeService = wxMaService.getSubscribeService();
            subscribeService.sendSubscribeMsg(templateMessage);
            return true;
        } catch (WxErrorException e) {
            // 处理异常情况
            log.error("小程序消息发送失败:{}", e.getMessage());
            e.printStackTrace();
            return false;
        }
    }
}

调用示例

import com.kjhd.service.util.WeChatNotifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/****
 *测试消息推送
 *****/
@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    WeChatNotifyService weChatNotifyService;

    @GetMapping()
    public void test() {
        String page = "pages/index";
        String openId="oZVN50VZ5QwKZqx60_83K4Xc0QD";
        String templateId = "123456789";
        HashMap<String, String> map = new HashMap<>();
        map.put("thing1", "111");
        map.put("time2", "2024-04-04 12:00:00");
        map.put("thing3", "内容");
        Boolean boo = weChatNotifyService.sendMiniAppNotification(openId, templateId, page, map);
    }
}

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值