java实现企业微信消息推送功能的详细步骤-亚博电竞手机版

第一步:申请企业微信注册企业(链接:https://work.weixin.qq.com/nl/sem/registe?s=c&from=1011017189&bd_vid=11628667012427618020)

第二步:登录自己的企业微信找到应用管理———>添加应用

第三步:获取到应用的agentid、secret、企业id

第四步,准备代码编写:

model层代码:

package com.toone.itop.formula.function.inte.model; /** * @desc : 微信通用接口凭证 * */ public class accesstoken { // 获取到的凭证 private string token; // 凭证有效时间,单位:秒 private int expiresin; public string gettoken() { return token; } public void settoken(string token) { this.token = token; public int getexpiresin() { return expiresin; public void setexpiresin(int expiresin) { this.expiresin = expiresin; }

package com.toone.itop.formula.function.inte.model; /** * 消息基类(企业号 -> 普通用户) * */ public class basemessage { // 否 成员id列表(消息接收者,多个接收者用‘|'分隔,最多支持1000个)。特殊情况:指定为@all,则向该企业应用的全部成员发送 private string touser; // 否 部门id列表,多个接收者用‘|'分隔,最多支持100个。当touser为@all时忽略本参数 private string toparty; // 否 标签id列表,多个接收者用‘|'分隔,最多支持100个。当touser为@all时忽略本参数 private string totag; // 是 消息类型 private string msgtype; // 是 企业应用的id,整型。可在应用的设置页面查看 private int agentid; public string gettouser() { return touser; } public void settouser(string touser) { this.touser = touser; public string gettoparty() { return toparty; public void settoparty(string toparty) { this.toparty = toparty; public string gettotag() { return totag; public void settotag(string totag) { this.totag = totag; public string getmsgtype() { return msgtype; public void setmsgtype(string msgtype) { this.msgtype = msgtype; public int getagentid() { return agentid; public void setagentid(int agentid) { this.agentid = agentid; }

package com.toone.itop.formula.function.inte.model; /** * 文本 * */ public class text { //是 消息内容,最长不超过2048个字节 private string content; public string getcontent() { return content; } public void setcontent(string content) { this.content = content; }

package com.toone.itop.formula.function.inte.model; /** * 文本消息 * */ public class textmessage extends basemessage { // 文本 private text text; // 否 表示是否是保密消息,0表示否,1表示是,默认0 private int safe; public text gettext() { return text; } public void settext(text text2) { this.text = text2; } public int getsafe() { return safe; } public void setsafe(int safe) { this.safe = safe; } }

通用工具类:

package com.toone.itop.formula.function.inte.util; import java.security.cert.certificateexception; import java.security.cert.x509certificate; import javax.net.ssl.x509trustmanager; /** * 证书信任管理器(用于https请求 * */ public class myx509trustmanager implements x509trustmanager { public void checkclienttrusted(x509certificate[] chain, string authtype) throws certificateexception { } public void checkservertrusted(x509certificate[] chain, string authtype) throws certificateexception { public x509certificate[] getacceptedissuers() { return null; }

package com.toone.itop.formula.function.inte.util; /** * 企业微信参数 * */ public class wechatparamesutil { // 1.微信参数 // 企业id public final static string corpid = "ww0b7de3b4c25ba7881"; // 企业应用私钥oa public final static string corpsecret = "xbv7an7mev8yqsnszzzsn0l_ccnotjxbo9gvzr7obpy1"; // 企业应用的id public final static int agentid = 1000008; public final static string aws6url = "http://localhost:8088"; }

package com.toone.itop.formula.function.inte.util; import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import java.io.outputstream; import java.net.connectexception; import java.net.url; import javax.net.ssl.httpsurlconnection; import javax.net.ssl.sslcontext; import javax.net.ssl.sslsocketfactory; import javax.net.ssl.trustmanager; import net.sf.json.jsonexception; import net.sf.json.jsonobject; import com.toone.itop.formula.function.inte.model.accesstoken; public class wechatutil { // 微信的请求url // 获取access_token的接口地址(get) 限200(次/天) public final static string access_token_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}"; /** * 1.发起https请求并获取结果 * * @param requesturtcmhsbyzol * 请求地址 * @param requestmethod * 请求方式(get、post) * @param outputstr * 提交的数据 * @return jsonobject(通过jsonobject.get(key)的方式获取json对象的属性值) */ public static jsonobject httprequest(string requesturl, string requestmethod, string outputstr) { jsonobject jsonobject = null; stringbuffer buffer = new stringbuffer(); try { // 创建sslcontext对象,并使用我们指定的信任管理器初始化 trustmanager[] tm = { new myx509trustmanager() }; sslcontext sslcontext = sslcontext.getinstance("ssl", "sunjsse"); sslcontext.init(null, tm, new java.security.securerandom()); // 从上述sslcontext对象中得到sslsocketfactory对象 sslsocketfactory ssf = sslcontext.getsocketfactory(); url url = new ; httpsurlconnection httpurlconn = (httpsurlconnection) url.openconnection(); httpurlconn.setsslsocketfactory(ssf); httpurlconn.setdooutput(true); httpurlconn.setdoinput(true); httpurlconn.setusecaches(false); // 设置请求方式(get/post) httpurlconn.setrequestmethod(requestmethod); if ("get".equalsignorecase(requestmethod)) httpurlconn.connect(); // 当有数据需要提交时 if (null != outputstr) { outputstream outputstream = httpurlconn.getoutputstream(); // 注意编码格式,防止中文乱码 outputstream.write(outputstr.getbytes("utf-8")); outputstream.close(); } // 将返回的输入流转换成字符串 inputstream inputstream = httpurlconn.getinputstream(); inputstreamreader inputstreamreader = new inputstreamreader(inputstream, "utf-8"); bufferedreader bufferedreader = new bufferedreader(inputstreamreader); string str = null; while ((str = bufferedreader.readline()) != null) { buffer.append(str); bufferedreader.close(); inputstreamreader.close(); // 释放资源 inputstream.close(); inputstream = null; httpurlconn.disconnect(); jsonobject = jsonobject.fromobject(buffer.tostring()); } catch (connectexception ce) { // weixin server connection timed out } catch (exception e) { // https request error:{} // e.printstacktrace(); } return jsonobject; } * 2.获取access_token * @param appid * 凭证 * @param appsecret * 密钥 * @return public static accesstoken getaccesstoken(string appid, string appsecret) { accesstoken accesstoken = null; string requesturl = access_token_url.replace("{corpid}", appid).replace("{corpsecret}", appsecret); jsonobject jsonobject = httprequest(requesturl, "get", null); // 如果请求成功 if (null != jsonobject) { try { accesstoken = new accesstoken(); accesstoken.settoken(jsonobject.getstring("access_token")); accesstoken.setexpiresin(jsonobject.getint("expires_in")); } catch (jsonexception e) { accesstoken = null; // 获取token失败 // log.error("获取token失败 errcode:{} errmsg:{}" // jsonobject.getint("errcode") jsonobject.getstring("errmsg")); return accesstoken; }

service层:

package com.toone.itop.formula.function.inte.service; import net.sf.json.jsonobject; import com.google.gson.gson; import com.toone.itop.formula.function.inte.model.text; import com.toone.itop.formula.function.inte.model.textmessage; import com.toone.itop.formula.function.inte.util.wechatparamesutil; import com.toone.itop.formula.function.inte.util.wechatutil; /** * @desc : 发送消息 * */ public class wechatservice { private static string sendmessage_url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=access_token"; /** * @desc :0.公共方法:发送消息 * @param accesstoken * @param message * void */ public static string sendmessage(string uid, string content) { // 1.获取access_token:根据企业id和应用密钥获取access_token,并拼接请求url string accesstoken = wechatutil.getaccesstoken(wechatparamesutil.corpid, wechatparamesutil.corpsecret).gettoken(); // 2.获取发送对象,并转成json gson gson = new gson(); textmessage message = new textmessage(); // 1.1非必需 message.settouser(uid); // 不区分大小写 //message.settoparty("1"); //message.gettouser(totag); // txtmsg.setsafe(0); // 1.2必需 message.setmsgtype("text"); message.setagentid(wechatparamesutil.agentid); text text = new text(); text.setcontent(content); message.settext(text); string jsonmessage = gson.tojson(message); // 3.获取请求的url string url = sendmessage_url.replace("access_token", accesstoken); // 4.调用接口,发送消息 jsonobject jsonobject = wechatutil.httprequest(url, "post", jsonmessage); // 4.错误消息处理 if (null != jsonobject) { if (0 != jsonobject.getint("errcode")) { system.out.println("消息发送失败 errcode:{} errmsg:{}" jsonobject.getint("errcode") jsonobject.getstring("errmsg")); } } return jsonobject.tostring(); } public static void main(string[] args) { // 0.设置消息内容 string content = "这是一条测试消息"; //userid为企业用户的id string userid = "qianxia"; // 3.发送消息:调用业务类,发送消息 wechatservice.sendmessage(userid, content); } }

所需引入的依赖

测试效果图:

到此这篇关于java实现企业微信消息推送功能的文章就介绍到这了,更多相关java企业微信消息推送内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

展开全文
内容来源于互联网和用户投稿,文章中一旦含有亚博电竞手机版的联系方式务必识别真假,本站仅做信息展示不承担任何相关责任,如有侵权或涉及法律问题请联系亚博电竞手机版删除

最新文章

网站地图