Home > WeChat Applet > WeChat Development > Implementation of push messages in WeChat development

Implementation of push messages in WeChat development

高洛峰
Release: 2017-03-11 14:10:37
Original
1455 people have browsed it

This article mainly introduces the relevant information on WeChat development message push implementation code. Friends who need it can refer to it

Recently doing the development of WeChat public accounts, there is a need for such message push, with text Send the edited message to an application group in the WeChat enterprise account. Take notes here. The following is the organized content:


//定义数据模型
 
public class Access_token
{
public Access_token()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
string _access_token;
string _expires_in;
 
///
 
 
/// 获取到的凭证
///
 
public string access_token
{
get { return _access_token; }
set { _access_token = value; }
}
 
 
///
 
 
/// 凭证有效时间,单位:秒
///
 
public string expires_in
{
get { return _expires_in; }
 
 
set { _expires_in = value; }
}
 
}
Copy after login


 public ActionResult index(string returnUrl)
{
 
GetAccess_token();
 
IsExistAccess_Token();
 
return View();
}
 
 
 
public static Access_token GetAccess_token()
{
string AppUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?";
string AppID = "应用组的CorpID";//在设置-》权限管理-》系统管理组
string AppSecret = "应用组的Secret";//在设置-》权限管理-》系统管理组
WebClient webClient = new WebClient();
Byte[] bytes = webClient.DownloadData(string.Format("{0}corpid={1}&corpsecret={2}", AppUrl, AppID, AppSecret));
string result = Encoding.GetEncoding("utf-8").GetString(bytes);
JObject jObj = JObject.Parse(result);
 
string token = jObj["access_token"].ToString();
string expires_in = jObj["expires_in"].ToString();
 
Access_token mode = new Access_token();
mode.access_token = token;
mode.expires_in = expires_in;
return mode;
}
 
///
Copy after login

Determine whether the Access_Token has expired based on the current date. If it expires, return a new Access_Token, otherwise return the previous Access_Token


public static string IsExistAccess_Token()
{
string Token = string.Empty;
DateTime YouXRQ;
string strPath = "../../weixin/XMLFile.xml";
// 读取XML文件中的数据,并显示出来
//string filepath = System.Web.Hosting.HostingEnvironment.MapPath(strPath);
string filepath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
 
StreamReader str = new StreamReader(filepath, System.Text.Encoding.UTF8);
XmlDocument xml = new XmlDocument();
xml.Load(str);
str.Close();
str.Dispose();
Token = xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText;
YouXRQ = Convert.ToDateTime(xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText);
 
 
if (DateTime.Now > YouXRQ)
{
DateTime _youxrq = DateTime.Now;
Access_token mode = GetAccess_token();
xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText = mode.access_token;
_youxrq = _youxrq.AddSeconds(int.Parse(mode.expires_in));
xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText = _youxrq.ToString();
xml.Save(filepath);
Token = mode.access_token;
}
 
object text = new
{
toparty = "1",
agentid = "2",
msgtype = "text",
text = new
{
content = "项目名称:"+来保网+""
}
};
 
string wcr= btnSend(Token, text);
return wcr;
 
}
public static string btnSend(string Token, object text)
{
 
string url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + Token;
WebRequest req = WebRequest.Create(url);
JavaScriptSerializer aa = new JavaScriptSerializer();
string postData = aa.Serialize(text);
byte[] requestBytes = Encoding.UTF8.GetBytes(postData);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = requestBytes.Length;
 
Stream requestStream = req.GetRequestStream();
requestStream.Write(requestBytes, 0, requestBytes.Length);
requestStream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.Default);
string backstr = sr.ReadToEnd();
sr.Close();
res.Close();
WeChatReturn WCR = aa.Deserialize(backstr);
return WCR.errmsg;
 
}
Copy after login

Thanks for reading, hope it helps To everyone, thank you for your support of this site!

The above is the detailed content of Implementation of push messages in WeChat development. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template