c# applet subscription notification push

Before the applet sends subscription notifications, it needs user authorization to send it.

Mini program code:

note

1. You can call up to three for each authorization call. If there are multiple notification templates, you can call three when you click the a button, and you can call the other three when you click the b button.

2. There are two types of small program authorization notifications, namely:

[One-time subscription] After the user subscribes independently, the developer can send a corresponding service message without time limit; each message can be subscribed or unsubscribed separately.

[Long-term subscription messages] One-time subscription messages can meet the needs of most service scenarios of mini programs, but there are scenarios in offline public services that cannot be met by one-time subscriptions. For example, if a flight is delayed, you need to send multiple messages based on the real-time flight status remind. For the convenience of service, we provide a long-term subscription message. After the user subscribes once, the developer can send multiple messages for a long time.

At present, long-term subscription news is only open to offline public services such as government affairs and people's livelihood, medical care, transportation, finance, education, etc., and will gradually support other offline public service businesses in the future

Therefore, most small programs only have the "one-time subscription" function. Only the user clicks on authorization to send a corresponding service message. If there are multiple messages to be pushed, the authorization page must be jumped out multiple times to allow the user to authorize. This is a headache

Official link: https://developers.weixin.qq.com/doc/offiaccount/Message_Management/One-time_subscription_info.html

Reference: https://developers.weixin.qq.com/community/develop/doc/00008a8a7d8310b6bf4975b635a401?blockType=1

Development part

//openid为接收订阅通知用户的openid
//username根据你模板参数来
//repairtime根据你模板参数来
  public void SendSubscribe_repair(string openid, string username, DateTime repairtime, string address, string remarks)
        {
            string access_token = GetAccess_token();//Access_Token,这边写个方法,调用接口
            string _url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + access_token;
            string jsonParam = "{\"touser\": \"" + openid + "\"," +
                "\"template_id\": \""+Constant.KL_SUBSCRIBE_BAOXIU + "\"," +
                "\"data\": {\"thing4\": { \"value\": \"" + username + "\"}," +
                "\"time5\": {\"value\": \"" + repairtime + "\"}" +
                ",\"thing2\": {\"value\": \"" + address + "\"}," +
                "\"thing3\": {\"value\": \"" + remarks + "\"}}}";
                //注意jsonParam 的数据为模板的格式,这个就看你微信平台如何设置的了
                
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url);
            request.Method = "POST";
            request.Timeout = 5000;
            request.ContentType = "application/json;charset=UTF-8";
            byte[] byteData = Encoding.UTF8.GetBytes(jsonParam);
            int length = byteData.Length;
            request.ContentLength = length;
            using (Stream writer = request.GetRequestStream())
            {
                writer.Write(byteData, 0, length);
                writer.Close();
            }
            string jsonStrings = string.Empty;
            using (HttpWebResponse responses = (HttpWebResponse)request.GetResponse())
            {
                using (Stream streams = responses.GetResponseStream())
                {
                    using (StreamReader readers = new StreamReader(streams, System.Text.Encoding.UTF8))
                    {
                        jsonStrings = readers.ReadToEnd();
                        responses.Close();
                        streams.Close();
                        readers.Close();
                    }
                }
            }
            //这里是返回的数据
            JObject jo = (JObject)JsonConvert.DeserializeObject(jsonStrings);
            string errcode = jo["errcode"].ToString();
            string errmsg = jo["errmsg"].ToString();
        }

Get access_token method

  private string GetAccess_token()
        {
            string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="小程序appid"&secret="小程序appsecret";
            System.Net.WebClient wCient = new System.Net.WebClient();
            wCient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            byte[] postData = System.Text.Encoding.UTF8.GetBytes("");
            byte[] responseData = wCient.UploadData(url, "POST", postData);
            string returnStr = System.Text.Encoding.UTF8.GetString(responseData);//返回接受的数据 
            JObject obj = (JObject)JsonConvert.DeserializeObject(returnStr);
            string access_token = obj["access_token"].ToString();
            return access_token;
        }

Note that the data of jsonParam is in the format of a template, this depends on how your WeChat platform is set

 

note

1. The part of the red box in the above picture is such as: thing4 cannot be modified, it must be strictly in accordance with the template

2. First post several situations of return information encountered

  • {“errcode”:43101,“errmsg”:“user refuse to accept the msg hint: [e5WcGA07873114]”}

The user refuses to accept the message. If the user has subscribed before, it means that the user has cancelled the subscription relationship. If the user only subscribes once, the second transmission will also have this error

  • {“errcode”:47003,“errmsg”:“argument invalid! hint: [26.orA01123945] data.character_string1.value i”}

The template parameter is inaccurate, it may be empty or does not meet the rules, errmsg will prompt which field is wrong and the parameter name is written in the applet background template.

  • {“errcode”:0,“errmsg”:“ok”}

success

Those who are interested can pay attention to " Ink Direct ", there are many free programming materials to receive~

Guess you like

Origin blog.csdn.net/huxinyu0208/article/details/109345439