小程序订阅消息_关于后台微信小程序订阅消息推送Demo

本文介绍了如何进行微信小程序订阅消息的后台推送,包括 Maven 引入相关包,设置小程序配置参数,前端订阅消息的实现,以及后台如何触发消息推送。
摘要由CSDN通过智能技术生成

65fc3546cab5cb3c62dd7bae31fcd77f.png

1.maven引入包 貌似是4以上的包才可以 记得关注包的版本号

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

2.初始化小程序配置参数

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaMsgServiceImpl;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.chinacreator.c2.config.ConfigManager;
import com.chinacreator.c2.web.exception.InvalidRestParamException;
import com.chinacreator.eamp.logisticsinternet.core.utils.ToolUtil;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 */
@Configuration
public class WxMaWxMaConfiguration {

    private static WxMaService wxService = null;

    public static WxMaService getMaService() {
        if (wxService == null) {
            throw new InvalidRestParamException(String.format("未找到微信小程序的配置"));
        }
        return wxService;
    }

    @PostConstruct
    public void init() throws Exception{
        //接入订阅消息推送 微信小程序包从3.4升级到3.7
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(ConfigManager.getInstance().getConfig("wxmini_appId"));
        config.setSecret(ConfigManager.getInstance().getConfig("wxmini_secretKey"));
        config.setToken(ConfigManager.getInstance().getConfig(""));
        config.setAesKey(ConfigManager.getInstance().getConfig(""));
        config.setMsgDataFormat("JSON");
//        String port = ConfigManager.getInstance().getConfig("wxmini_http_proxy_port");
//        String host = ConfigManager.getInstance().getConfig("wxmini_http_proxy_host");
//        //内网请求微信通过这种代理服务
//        if(StringUtils.isNotBlank(port) && StringUtils.isNotBlank(port)){
//            config.setHttpProxyPort(Integer.parseInt(port));
//            config.setHttpProxyHost(host);
//        }
        wxService = new WxMaServiceImpl();
        wxService.setWxMaConfig(config);
    }

}

3.小程序端消息订阅 这个可参考前端api

4.后台消息推送方法

//发送订阅消息
    public Map sendSubscribeMsg(Map<String,String> content){
        Map<String,String> result = new HashMap();
        String code = "0";
        String errMsg = null;
        try {
            //组装消息主体
            WxMaSubscribeMessage subscribeMessage = new WxMaSubscribeMessage();
            subscribeMessage.setTemplateId(订阅消息模板ID);
            subscribeMessage.setToUser(小程序用户OpenId);
            content = new HashMap<>();
            content.put("examineContent","农产品审核");
            content.put("examineResult","农产品");
            content.put("examineTime","2020-10-16 15:35");
            content.put("applyName","张义");
            content.put("applyTime","2020-10-10 10:35");

            List<WxMaSubscribeMessage.Data> msgContent = new ArrayList<>();
            /**
             * 审核内容
             * {{thing7.DATA}}
             *
             * 审核结果
             * {{thing2.DATA}}
             *
             * 审核时间
             * {{date3.DATA}}
             *
             * 申请人
             * {{name4.DATA}}
             *
             * 申请时间
             * {{time14.DATA}}
             */
            String template = "{"thing7":"examineContent","thing2":"examineResult","date3":"examineTime","name4":"applyName","time14":"applyTime"}";
            Map<String,String> teamplateMap = ToolUtil.jsonToBean(template,Map.class);

            //组装消息内容
            for(String key: teamplateMap.keySet()){
                String valueKey = teamplateMap.get(key);
                WxMaSubscribeMessage.Data msgElement = new WxMaSubscribeMessage.Data();
                msgElement.setName(key);
                msgElement.setValue(content.get(valueKey));
                msgContent.add(msgElement);
            }
            subscribeMessage.setData(msgContent);
            subscribeMessage.setPage("/produce/home/index");

            //发送消息
            WxMaMsgServiceImpl msg = new WxMaMsgServiceImpl(WxMaWxMaConfiguration.getMaService());
            msg.sendSubscribeMsg(subscribeMessage);
            code = "1";
        } catch (WxErrorException e) {
            e.printStackTrace();
            code = "-1";
            errMsg = e.getMessage();
        }
        result.put("code",code);
        result.put("errMsg",errMsg);
        return result;
    }
一、rocketmq入门到精通视频教程目录大纲 001-001_RocketMQ_简介 002-002_RocketMQ_核心概念详解 003-003_RocketMQ_集群构建模型详解(一) 004-004_RocketMQ_集群构建模型详解(二) 005-005_RocketMQ_双主模式集群环境搭建 006-006_RocketMQ_控制台使用讲解 007-007_RocketMQ_Broker配置文件详解 008-008_RocketMQ_helloworld示例讲解 009-009_RocketMQ_整体架构概述详解 010-010_RocketMQ_Producer_API详解 011-011_RocketMQ_Producer_顺序消费机制详解 012-012_RocketMQ_Producer_事务消息机制详解 013-013_RocketMQ_Consumer_Push和Pull模式及使用详解 014-014_RocketMQ_Consumer_配置参数详解 015-015_RocketMQ_Consumer_重试策略详解 016-016_RocketMQ_Consumer_幂等去重策略详解 017-017_RocketMQ_消息模式 及使用讲解 018-018_RocketMQ_双主双从集群环境搭建与使用详解 019-019_RocketMQ_FilterServer机制及使用详解 020-020_RocketMQ_管理员命令 二、rocketmq实战视频教程目录大纲 01_rocketmq_实战项目介绍 02_rocketMQ实战项目设计(一) 03_rocketMQ实战项目设计(二) 04_rocketMQ实战-环境搭建(一) 05_rocketMQ实战-环境搭建(二) 06_rocketMQ实战-生产者与spring结合 07_rocketMQ实战-消费者与spring结合 08_rocketMQ实战-数据库模型设计 09_rocketMQ实战-数据库DAO代码生成 10_rocketMQ实战-远程RPC接口设计与实现(一) 11_rocketMQ实战-远程RPC接口设计与实现(二) 12_rocketMQ实战-远程RPC接口设计与实现(三) 13_rocketMQ实战-下单流程(一) 14_rocketMQ实战-下单流程(二) 15_rocketMQ实战-下单流程(三) 16_rocketMQ实战-下单流程(四) 17_rocketMQ实战-下单流程(五) 18_rocketMQ实战-下单流程(六) 19_rocketMQ实战-下单流程(七) 20_rocketMQ实战-下单流程(八)-商品库存 21_rocketMQ实战-下单流程(九)-商品库存 22_rocketMQ实战-下单流程(十)-支付模块 23_rocketMQ实战-整体联调
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值