微信小程序向公众号推送消息

微信小程序向公众号推送消息

PS:小程序推送一次性订阅消息需要每一次都授权,长期订阅消息应用领域有限,不太好申请,公众号和小程序绑定,当小程序需要发消息时可以直接推送至公众号,限制较少

步骤一:开通一下服务号公众号,关联小程序!!!

步骤二:开通公众号模板消息

在这里插入图片描述

前提:
 private final static String OFFICIAL_ACCOUNT_APPID = "xxxx";                                  // 微信公众号appid
 private final static String OFFICIAL_ACCOUNT_TEMPLATE_ID = "xxxx-63nf2pS-SnrZCroNY";   // 微信公众号模板消息id
 private final static String OFFICIAL_ACCOUNT_URL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token="; // 微信公众号统一订阅消息URL

步骤三:

 public static void pushWeChatMessage(String openId, MessageInfoDto infoDto) {
        log.writeLog("wechat pushSubscribeMessage start...");
        String accessToken = WeChatHelper.getAccessToken();
        if (StringUtils.isEmpty(openId) || StringUtils.isEmpty(accessToken)) {
            log.writeLog("openid not exists", ApiErrorCode.FORBIDDEN.getMessage());
        }
        String nodename = "流程已到达\"" + infoDto.getNodename() + "\"节点";
        System.out.println("nodename = " + nodename);
        // 封装参数,根据模版内容动态调整
        JSONObject requestParams = new JSONObject();
        requestParams.put("touser", openId);
        requestParams.put("mp_template_msg", new JSONObject()
                .fluentPut("appid", OFFICIAL_ACCOUNT_APPID)                 // 公众号appid,要求与小程序有绑定且同主体
                .fluentPut("template_id", OFFICIAL_ACCOUNT_TEMPLATE_ID)     // 公众号模板id
                .fluentPut("miniprogram", new JSONObject().fluentPut("appid", MINI_APP_ID)) // 公众号模板消息所要跳转的小程序id
                .fluentPut("data", new JSONObject()
                        .fluentPut("first", new JSONObject().fluentPut("value", nodename))
                        .fluentPut("keyword1", new JSONObject().fluentPut("value", infoDto.getRequestname()))
                        .fluentPut("keyword2", new JSONObject().fluentPut("value", infoDto.getUsername()))
                        .fluentPut("keyword3", new JSONObject().fluentPut("value", infoDto.getReceivedatetime()))
                        .fluentPut("remark", new JSONObject().fluentPut("value", "remark"))));
        log.writeLog("requestParams: " + requestParams.toJSONString());
        System.out.println("requestParams.toJSONString() = " + requestParams.toJSONString());
        String result = HttpUtil.post(OFFICIAL_ACCOUNT_URL + accessToken, requestParams.toJSONString());
        System.out.println("result = " + result);
        log.writeLog("push message result: " + result);
        log.writeLog("wechat pushSubscribeMessage end...");
    }
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr朱墨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值