点个赞哦
背景:
最近开发的一个小程序项目需要通过服务号来推送通知。但是在最开始开发小程序的时候使用了一次性消息推送,不是很友好。
在做消息推送时也是踩了无数坑,因为在2021年2月前其他人的文章所使用的案例中调用的小程序模板消息接口都没有用,也没有实质性的帮助,所以我在这篇文章中给出自己摸索出针对统一消息接口做推送的解决方案。
1.首先将小程序绑定到指定的公众号下
2.申请公众号模板
3.yml中增加小程序和公众号id及消息模板配置
4.增加配置类
@Data
@Component
@ConfigurationProperties()
public class WxMiniConfigVo {
/**小程序appid*/
@Value("${wxmini.id}")
private String WxMiniAPPID;
/**小程序秘钥*/
@Value("${wxmini.secret}")
private String WxMiniSECRET;
/**小程序数据加密方式*/
@Value("${wxmini.grant_type}")
private String WxMinigrantType;
/**公众号id*/
@Value("${wxmini.gzh_id}")
private String WxGzhId;
/**公众号模板*/
@Value("${wxmini.gzh_template}")
private String WxGzhTemplate;
}
5.编写接口
@Slf4j
@Service
public class SendMiniAppServiceImpl implements SendMiniAppService {
@Autowired
private WxMiniConfigVo wxMiniConfigVo;
@Autowired
private RedisUtil redisUtil;
@Override
public String getAccessToken() {
RestTemplate restTemplate = new RestTemplate();
Map<String, String> params = new HashMap<>();
params.put("APPID", wxMiniConfigVo.getWxMiniAPPID());
params.put("APPSECRET", wxMiniConfigVo.getWxMiniSECRET());
params.put("grant_type", "client_credential");
String tokenUrl="https://api.weixin.qq.com/cgi-bin/token?appid={APPID}&secret={APPSECRET}&grant_type={grant_type}";
ResponseEntity<String> responseEntity = restTemplate.getForEntity(tokenUrl, String.class, params);
String body = responseEntity.getBody();
JSONObject object = JSON.parseObject(body);
String Access_Token = object.getString("access_token");
redisUtil.set("wxMiniToken", Access_Token);
redisUtil.expire("wxMiniToken", Long.parseLong(object.getString("expires_in")));
return Access_Token;
}
@Override
public String push(String openid, Map<String, String> content) {
//获取用户token
String token = String.valueOf(redisUtil.get("wxMiniToken"));
if (token == "null") {
token = getAccessToken();
}
String resultStatus = "0";//0:失败,1:成功
try {
//小程序统一消息推送
String path = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=" + token;
//封装参数
JSONObject jsonData = new JSONObject();
jsonData.put("touser", openid); // 小程序openid
JSONObject jsonObject = new JSONObject();
jsonObject.put("appid", wxMiniConfigVo.getWxGzhId());// 公众号id
jsonObject.put("template_id", wxMiniConfigVo.getWxGzhTemplate());// 公众号消息模板id
//公众号消息数据封装
JSONObject data = new JSONObject();
data.put("first", getValue("请注意!有新告警产生"));//标题
data.put("keyword1", getValue(content.get("deviceId")));//设备编号
data.put("keyword2", getValue(content.get("alarmName")));//设备名称
data.put("keyword3", getValue(content.get("alarmAddress")));//设备地址
data.put("keyword4", getValue(content.get("content")));//告警描述
data.put("keyword5", getValue(content.get("alarmTime")));//告警时间
data.put("remark", getValue("请及时排查并处理!"));
jsonObject.put("data", data);
jsonData.put("mp_template_msg", jsonObject);
HttpRequest.sendPost(path, jsonData.toJSONString());
resultStatus="1";
} catch (Exception e) {
log.error("微信公众号发送消息失败!",e.getMessage());
resultStatus="0";
}
return resultStatus;
}
private JSONObject getValue(String value) {
// TODO Auto-generated method stub
JSONObject json = new JSONObject();
json.put("value", value);
json.put("color", "#173177");
return json;
}