官方文档有点看不进去,找了各种资源,又容易入坑,仅实现微信推送预警消息到用户功能。自我记录,不喜勿喷。实现效果。
API: http://mp.weixin.qq.com/wiki/home/index.html
接口调试工具:http://mp.weixin.qq.com/debug
微信公众号推送消息的实现:参考可用,按照步骤申请测试号进行测试即可。
https://blog.csdn.net/qiushi_1990/article/details/88864527
在微信公众平台中获取appid和appsecret信息:
改为正式使用的微信公众号的时候,需要获取token和openid信息,可以直接复制链接到浏览器访问,得到返回值进行测试。
1. 获取token:(GET)
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET
请求参数:
grant_type |
是 |
获取access_token填写client_credential |
appid |
是 |
第三方用户唯一凭证 |
secret |
是 |
第三方用户唯一凭证密钥,即appsecret |
返回值:
{"access_token":"ACCESS_TOKEN","expires_in":7200}
得到了ACCESS_TOKEN值。
2、获取人员Openid
1获取全部用户。
https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID
获取全部的时候,可以将后面的信息去掉。
https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN
参数 |
是否必须 |
说明 |
access_token |
是 |
调用接口凭证 |
next_openid |
是 |
第一个拉取的OPENID,不填默认从头开始拉取 |
正确时返回JSON数据包:
{"total":2,"count":2,"data":{"openid":["","OPENID1","OPENID2"]},"next_openid":"NEXT_OPENID"}
例子:
换成自己账号上一步获取的token。
1.pom.xml中引入以下信息:开源binarywang库。
<!--微信模版消息推送三方sdk-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>3.3.0</version>
</dependency>
2.Java后台代码实现:(需要换成你自己的信息)
package com.dclw.Controller;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
/**
* desc: 模版消息推送模拟
*/
@RestController
public class PushController {
/*
* 微信测试账号推送
* */
@GetMapping("/push")
public void push() {
//1,配置
WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
wxStorage.setAppId("wxa14a0f6f44cace6c66");//换成你自己的
wxStorage.setSecret("0b35d1783fd44a7bd3fd156dbf5e93477885");//换成你自己的
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxStorage);
//2,推送消息
WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
.toUser("oU_26wu9DJh13khEdCcSaj66SbF_KQ")//要推送的用户openid(//换成你自己的)
.templateId("IZGVGBieBqvR4OZDUQmG0hlPRj9L-OJaSVsLazyo66wnw")//模版id(//换成你自己的)
.url("http://hsfxHenanFront/user/login")//点击模版消息要访问的网址
.build();
//3,如果是正式版发送模版消息,这里需要配置你的信息
// {{first.DATA}}
// 预警位置:{{keyword1.DATA}}
// 预警等级:{{keyword2.DATA}}
// {{remark.DATA}}
templateMessage.addData(new WxMpTemplateData("first","重要信息提示", "#FF00FF"));
templateMessage.addData(new WxMpTemplateData("keyword1","科实大厦雨量站点", "#FF00FF"));
templateMessage.addData(new WxMpTemplateData("keyword2","红色预警", "#FF00FF"));
templateMessage.addData(new WxMpTemplateData("remark","请及时查看!", "#FF00FF"));
try {
wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
} catch (Exception e) {
System.out.println("推送失败:" + e.getMessage());
e.printStackTrace();
}
}
}
QQ:1764581679