900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 微信小程序向公众号推送消息

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

时间:2021-07-12 19:22:25

相关推荐

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

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

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

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

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

前提:private final static String OFFICIAL_ACCOUNT_APPID = "xxxx"; // 微信公众号appidprivate final static String OFFICIAL_ACCOUNT_TEMPLATE_ID = "xxxx-63nf2pS-SnrZCroNY"; // 微信公众号模板消息idprivate final static String OFFICIAL_ACCOUNT_URL = "https://api./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...");}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。