java实现微信公众号推送消息

微信公众平台
微信公众号开发文档
API地址
基本信息
AppID:开发者ID,微信公众号的唯一标识
AppSecret:开发者密码,操作微信公众号的验证
IP白名单:获取access_token时,需要IP白名单才可以获取
OpenID:微信用户在当前公众号的唯一标识
UnionID:微信用户在当前开放平台账号的所有公众号和应用情景下的唯一标识
服务器配置:
(1)地址:配置以后,用户对公众号的操作(关注、发消息),微信会转到该链接
(2)令牌:用作生成签名
(3)密钥:消息加密

微信测试号


        
            com.github.binarywang
            weixin-java-mp
            3.3.0
        

代码:

气象灾害预警提醒
{{first.DATA}}
发布单位:{{k1.DATA}}  
预警类别:{{k2.DATA}} 
预警级别:{{k3.DATA}}
发布时间:{{k4.DATA}}  
public void push() {
		//1,配置
		WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
		wxStorage.setAppId("wxxxx");
		wxStorage.setSecret("3xxxxabc1");
		WxMpService wxMpService = new WxMpServiceImpl();
		wxMpService.setWxMpConfigStorage(wxStorage);
		Alarm alarm = alarmRepository.getAlarm("2019070600_85");
		String color = "#FFA500";//橙色
		if(alarm.getColor().startsWith("蓝色")){
			color = "#001EFF";
		}else if(alarm.getColor().startsWith("红色")){
			color = "#FF0000";
		}else if(alarm.getColor().startsWith("黄色")){
			color = "#FFFF00";
		}
//		String alarmPath = "http://192.168.10.17:9091/"+alarm.getFilePath()+ File.separator+alarm.getPdfName();
		//2,推送消息
		WxMpTemplateMessage templateMessage = new WxMpTemplateMessage();
		templateMessage.setTemplateId("xxxxwhP8");
//				.url(alarmPath)//点击模版消息要访问的网址
		List wxMpTemplateData = new ArrayList<>();
		wxMpTemplateData.add(new WxMpTemplateData("first", alarm.getDESCRIPTION(), color));
		wxMpTemplateData.add(new WxMpTemplateData("k1", alarm.getSender(), color));
		wxMpTemplateData.add(new WxMpTemplateData("k2", alarm.getAlarmType(), color));
		wxMpTemplateData.add(new WxMpTemplateData("k3", alarm.getColor(), color));
		wxMpTemplateData.add(new WxMpTemplateData("k4", alarm.getAlarmTime(), color));
		templateMessage.setData(wxMpTemplateData);
		List openList = new ArrayList<>();
		try {
			openList = recursionOpenId(openList,null,wxMpService);
			for(String id :openList){
				templateMessage.setToUser(id);//要推送的用户openid
				wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
			}
		} catch (Exception e) {
			System.out.println("推送失败:" + e.getMessage());
			e.printStackTrace();
		}

	}

	/**
	 * 递归查询openId,一次最多拉取10000各个关注者的OpenId,可以多次拉取满足需求
	 * @param openIdList
	 * @param nextOpenId 第一次拉取的openId,不填默认从头开始拉取
	 * @param wxMpService
	 * @return
	 */
	public List recursionOpenId(List openIdList,String nextOpenId,WxMpService wxMpService){
		try {
			WxMpUserList openList = wxMpService.getUserService().userList(nextOpenId);
			for(String strId :openList.getOpenids()){
				openIdList.add(strId);
			}
			if(openList.getOpenids().size()>10000){
				recursionOpenId(openIdList,openList.getNextOpenid(),wxMpService);
			}
		} catch (WxErrorException e) {
			e.printStackTrace();
		}

		return openIdList;
	}

注意:变色仅限手机端,电脑端是不变色的。

获取黑名单

post方式
https://api.weixin.qq.com/cgi-bin/tags/members/getblacklist?access_token=xxx
{
	"begin_openid":"OPENID1"
}
当 begin_openid 为空时,默认从开头拉取。

过滤黑名单,否则推送预警报错

private List recursionOpenId(List openIdList,String nextOpenId,WxMpService wxMpService){
		try {
			WxMpUserList openList = wxMpService.getUserService().userList(nextOpenId);
			WxMpUserBlacklistGetResult blackResult = wxMpService.getBlackListService().getBlacklist("");
			List blackList = blackResult.getOpenidList();
			List allOpenIds = openList.getOpenids();
			allOpenIds.removeAll(blackList);
			for(String strId : allOpenIds){
				openIdList.add(strId);
			}
			if(allOpenIds.size()>10000){
				recursionOpenId(openIdList,openList.getNextOpenid(),wxMpService);
			}
		} catch (WxErrorException e) {
			e.printStackTrace();
		}

		return openIdList;
	}

获取用户信息

GET https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
WxMpUser user = wxMpService.getUserService().userInfo(openId);

你可能感兴趣的:(微信)