[微信小程序]向指定用户推送模版消息(c#代码)亲测可用

调用:SubscribeMessageToUser("用户openid字符串");
官方比较坑:限制必须点击授权按钮的时候才能执行否则就会提示:
{"errcode":43101,"errmsg":"user refuse to accept the msg rid: 5f66d001-785045b2-5443a9f9"}
翻译解释为:用户拒绝接收信息

小程序向指定用户推送模版消息(c#代码)      [HttpPost] public JsonResult SubscribeMessageToUser(string openid, string title=null, string content=null) { AjaxResult res = new AjaxResult(); res.state = false; try { AccessTokenModel AccessTokenModel = xcx_Get_Token();//获取access_token string _url = string.Format("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={0}", AccessTokenModel.access_token); //json参数 string jsonParam = Newtonsoft.Json.JsonConvert.SerializeObject(new { touser = openid,//用戶openid template_id = "模版ID", // 模版template_id page = "pages/index/index", //miniprogram_state = "developer", lang = "zh_CN", data = new { thing1 = new { value = "签到提醒" }, time5 = new { value = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") }, thing2 = new { value = "点击签到" } } }); var request = (HttpWebRequest)WebRequest.Create(_url); request.Method = "POST"; request.ContentType = "application/json;charset=UTF-8"; byte[] byteData = Encoding.UTF8.GetBytes(jsonParam); int length = byteData.Length; request.ContentLength = length; Stream writer = request.GetRequestStream(); writer.Write(byteData, 0, length); writer.Close(); var response = (HttpWebResponse)request.GetResponse(); var responseString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd(); JavaScriptSerializer js = new JavaScriptSerializer(); //实例化一个能够序列化数据的类 AccessTokenModel_Err ErrInfo = js.Deserialize<AccessTokenModel_Err>(responseString); //AccessTokenModel_Err ErrInfo = (AccessTokenModel_Err)JsonConvert.DeserializeObject(responseString); res.message = ErrInfo.errmsg; if(ErrInfo.errcode==0) res.state = true; //return responseString.ToString(); } catch (Exception ex) { res.state = false; res.message = ex.Message; } return Json(res, JsonRequestBehavior.AllowGet); }

  

posted @ 2020-09-20 11:41  大da脸  阅读(2235)  评论(0编辑  收藏  举报