接之前微信公众号的需求后,现在又需要对接微信小程序,实现订阅消息推送功能。
我把相关的实现逻辑,注意事项总结了一下。
一、配置信息
#################微信渠道-服务号##################
##granttype可共用
wechat.auth.granttype=client_credential
wechat.auth.token=Qwer@123
wechat.auth.appid=xxxxxxxxxx
wechat.auth.secret=xxxxxxxxxx
#获取AccessToken url GET
wechat.url.accesstoken=https://api.weixin.qq.com/cgi-bin/token
#模板消息推送 POST
wechat.url.template.send=https://api.weixin.qq.com/cgi-bin/message/template/send
#获取所有的消息模板 GET
wechat.url.get.all.template=https://api.weixin.qq.com/cgi-bin/template/get_all_private_template
#################微信渠道-服务号##################
#################微信渠道-小程序##################
#appId 和 secret 不可与公众号共用
weapp.auth.appid=xxxxxxxxxx
#小程序secret不再明文显示,如果小程序方重置,需要同步到消息中心
weapp.auth.secret=xxxxxxxxxxxxxxx
weapp.message.send=https://api.weixin.qq.com/cgi-bin/message/subscribe/send
#################微信渠道-小程序##################
注意事项:
1、微信公众号与微信小程序是否是同一个主体,我这里不是同一个主体,所以appid和secret是两个不同的配置。
2、获取token的granttype类型一致,可以共用。
获取token的方法可以参考之前的文章:https://blog.youkuaiyun.com/ILoveController/article/details/111642431
二、参数说明
根据官方文档说明,有几个必填字段

1、推送人openid,需要找小程序后端要一下订阅用户的openid
2、模板id,到小程序里查看需要发送的模板id
三、JAVA实现
String sendUrl = weAppSendUrl + "?access_token=" + accessToken;
JSONObject params = new JSONObject();
params.put("touser", touser);
params.put("template_id", templateId);
params.put("data", data);
if (StringUtils.isNotBlank(page)) {
params.put("page", page);
}
if (StringUtils.isNotBlank(miniProgramState)) {
params.put("miniprogram_state", miniProgramState);
}
if (StringUtils.isNotBlank(lang)) {
params.put("lang", lang);
}
//header
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.APPLICATION_JSON);
//HttpEntity
HttpEntity<JSONObject> httpEntity = new HttpEntity<>(params, header);
RestTemplate restTemplate = new RestTemplate();
//JSONObject数据结果
JSONObject result = restTemplate.postForObject(sendUrl, httpEntity, JSONObject.class);

2022

被折叠的 条评论
为什么被折叠?



