java实现小程序订阅通知
微信小程序消息订阅
前端
submit(){
uni.requestSubscribeMessage({
tmplIds:['你的订阅消息模版Id'],
success: res => {
console.log(res)
},
fail: res=>{
console.log(res)
}
});
},
这里做个绑定时间做测试。点击即可调用
后端先提前把获取openid 以及access_token的代码写好,这里不做过过多说明
后面就是调用接口去发送订阅消息代码
@GetMapping("sendSubscribeMessage")
public void sendSubscribeMessage() {
String result1 = HttpUtils.sendGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
+ appid + "&secret="
+ appsecret + "&js_code="
, null);
JSONObject jsonObject1 = JSON.parseObject(result1);
String accessToken = jsonObject1.getString("access_token").toString();
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;
JSONObject json1 = new JSONObject();
json1.put("touser", "用户的opeind");
json1.put("template_id", "消息模版Id");
json1.put("page", "index");
json1.put("lang", "zh_CN");
json1.put("miniprogram_state", "trial");
JSONObject data = new JSONObject();
data.put("character_string1", new JSONObject().fluentPut("value", "1234567890"));
data.put("name2", new JSONObject().fluentPut("value", "测试"));
data.put("thing3", new JSONObject().fluentPut("value", "测试"));
data.put("date4", new JSONObject().fluentPut("value", "2020-01-18 18:00"));
data.put("thing7", new JSONObject().fluentPut("value", "请抓紧去啊,不然打屁屁!"));
json1.put("data",data);
String result = HttpUtil.post(url, json1.toJSONString());
System.out.println("发送订阅消息结果:" );
更多推荐
所有评论(0)