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

Implementation of push messages in WeChat development

Mar 11, 2017 pm 02:10 PM
WeChat development Push message

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use Firebase Cloud Messaging (FCM) to implement message push functionality in PHP applications Use Firebase Cloud Messaging (FCM) to implement message push functionality in PHP applications Jul 24, 2023 pm 12:37 PM

Use Firebase Cloud Messaging (FCM) to implement message push function in PHP applications. With the rapid development of mobile applications, real-time message push has become one of the indispensable functions of modern applications. Firebase Cloud Messaging (FCM) is a cross-platform messaging service that helps developers push real-time messages to Android and iOS devices. This article will introduce how to use FCM to implement message push function in PHP applications.

How to implement message push and notification reminder in uniapp How to implement message push and notification reminder in uniapp Oct 20, 2023 am 11:03 AM

How to implement message push and notification reminders in uniapp With the rapid development of mobile Internet, message push and notification reminders have become indispensable functions in mobile applications. In uniapp, we can implement message push and notification reminders through some plug-ins and interfaces. This article will introduce a method to implement message push and notification reminder in uniapp, and provide specific code examples. 1. Message Push The premise for implementing message push is that we need a background service to send push messages. Here I recommend using Aurora Push.

How to use the PHP framework Lumen to develop an efficient message push system and provide timely push services How to use the PHP framework Lumen to develop an efficient message push system and provide timely push services Jun 27, 2023 am 11:43 AM

With the rapid development of mobile Internet and changes in user needs, the message push system has become an indispensable part of modern applications. It can realize instant notification, reminder, promotion, social networking and other functions to provide users and business customers with better services. experience and service. In order to meet this demand, this article will introduce how to use the PHP framework Lumen to develop an efficient message push system to provide timely push services. 1. Introduction to Lumen Lumen is a micro-framework developed by the Laravel framework development team. It is a

UniApp's design and development skills for implementing message push and push services UniApp's design and development skills for implementing message push and push services Jul 04, 2023 pm 12:57 PM

UniApp is a framework for developing cross-platform applications that can run on iOS, Android and Web platforms at the same time. When implementing the message push function, UniApp can cooperate with the back-end push service to realize the design and development of message push. 1. Design overview of message push To implement the message push function in UniApp, you need to design a push service to send push messages to the App. The push service needs to implement the following functions: establish a connection with the App and send messages.

Analysis of the relationship between PHP real-time communication function and message push middleware Analysis of the relationship between PHP real-time communication function and message push middleware Aug 10, 2023 pm 12:42 PM

Analysis of the relationship between PHP real-time communication function and message push middleware With the development of the Internet, the importance of real-time communication function in Web applications has become increasingly prominent. Real-time communication allows users to send and receive messages in real-time in applications, and can be applied to a variety of scenarios, such as real-time chat, instant notification, etc. In the field of PHP, there are many ways to implement real-time communication functions, and one of the common ways is to use message push middleware. This article will introduce the relationship between PHP real-time communication function and message push middleware, and how to use message push

How to turn off the message push on the Amap map_How to turn off the message push on the Amap map How to turn off the message push on the Amap map_How to turn off the message push on the Amap map Apr 01, 2024 pm 03:06 PM

1. Open the phone settings, click Applications, and click Application Management. 2. Find and click to enter the Amap. 3. Click Notification Management and turn off the Allow Notifications switch to turn off message push notifications. This article takes Honor magic3 as an example and is applicable to Amap v11.10 version of MagicUI5.0 system.

Quick Start: Use Go language functions to implement simple message push functions Quick Start: Use Go language functions to implement simple message push functions Jul 31, 2023 pm 02:09 PM

Quick Start: Use Go language functions to implement simple message push functions In today's mobile Internet era, message push has become a standard feature of various APPs. Go language is a fast and efficient programming language, which is very suitable for developing message push functions. This article will introduce how to use Go language functions to implement a simple message push function, and provide corresponding code examples to help readers get started quickly. Before we begin, we need to understand the basic principles of message push. Generally, message push functionality requires two main components: push server

PHP WeChat development: How to implement message encryption and decryption PHP WeChat development: How to implement message encryption and decryption May 13, 2023 am 11:40 AM

PHP is an open source scripting language that is widely used in web development and server-side programming, especially in WeChat development. Today, more and more companies and developers are starting to use PHP for WeChat development because it has become a truly easy-to-learn and easy-to-use development language. In WeChat development, message encryption and decryption are a very important issue because they involve data security. For messages without encryption and decryption methods, hackers can easily obtain the data, posing a threat to users.

See all articles