微信小程序订阅消息推送
微信小程序订阅消息推送
开发配置配置
服务器配置
- 在小程序后台开启消息推送(开发-开发设置-消息推送)文档地址
- 这里需要服务提供验证签名的接口 将下面代码发布到服务器并使用80或443端口访问(我在使用其他端口时出现问题 80和443端口正常) 例如:https://www.baidu.com/api/wx/CheckWxSignature
/// <summary> /// 接口认证 /// </summary> /// <param name="echostr">回传参数</param> /// <param name="signature">加密字段</param> /// <param name="timestamp">时间搓</param> /// <param name="nonce">随机字符串</param> /// <returns></returns> [HttpGet,Route("api/wx/CheckWxSignature")] public IActionResult CheckWxSignature(string echostr, string signature, string timestamp, string nonce) { string token = "test"; if (!CheckSignature(token, signature, timestamp, nonce)) { return Ok("error"); } return Ok(echostr); } /// <summary> /// 验证微信签名 /// </summary> private bool CheckSignature(string token, string signature, string timestamp, string nonce) { string[] ArrTmp = { token, timestamp, nonce }; Array.Sort(ArrTmp); string tmpStr = string.Join("", ArrTmp); var data = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(tmpStr)); var sb = new StringBuilder(); foreach (var t in data) { sb.Append(t.ToString("X2")); } tmpStr = sb.ToString(); tmpStr = tmpStr.ToLower(); if (tmpStr == signature) { return true; } else { return false; } }
小程序配置
小程序订阅消息在每次发送消息前需要询问用户是否授权,授权成功后才能调用。每次授权后只有一次调用权限。
服务器配置成功后还需要在小程序授权后才能调用,一次性授权的意思是授权后只有一次
的调用权限.
wx.requestSubscribeMessage({
tmplIds: ['template_id'], // 此处可填写多个模板 ID,但低版本微信不兼容只能授权一个
success (res) {
console.log('已授权接收订阅消息')
}
})
开发者工具目前无法调用接口,只能在真机上运行
服务端消息发送
WeChatHelper.cs
public class WeChatHelper{
private static string WXAppId="";
private static string WXAppSecret="";
/// <summary>
/// 对页面是否要用授权
/// </summary>
/// <param name="Appid">微信应用id</param>
/// <param name="redirect_uri">回调页面</param>
/// <param name="scope">应用授权作用域snsapi_base(不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息)</param>
/// <returns>授权地址</returns>
public static string GetToken()
{
string url = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", WXAppId, WXAppSecret);
string ReText = HttpClients.WebRequestPostOrGet(url, "");//post/get方法获取信息
try
{
var obj = JsonConvert.DeserializeObject<Tokens>(ReText);
return obj.access_token;
}
catch (Exception ex)
{
return "";
}
}
public static object SendMessage(string token)
{
var access_token = token;
string url = $"https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={access_token}";
var dic = new Dictionary<string,object>();//组装模板内容
dic.Add("character_string8",new { value = "2020071212121351" });//订单编号
dic.Add("amount5",new { value= "20.00" });//订单金额
dic.Add("time10", new { value = "2020-07-13 12:00:00" });//订单时间
dic.Add("name3", new { value = "商户名称" });//商户名称
dic.Add("phrase1", new { value = "已支付" });//订单状态
var data = new MessageData()
{
touser = "微信openid",
template_id = "模板id",
page = "需要跳转的小程序地址 不跳转就不填写",
data = dic
};
string ReText = HttpClients.WebRequestPostOrGet(url, JsonConvert.SerializeObject(data));//post/get方法获取信息
return ReText;
}
}
HttpClients.cs
public class HttpClients{
//发起Http请求
public static string HttpPost(string url, Stream data, IDictionary<object, string> headers = null)
{
System.Net.WebRequest request = HttpWebRequest.Create(url);
request.Method = "POST";
if (data != null)
request.ContentLength = data.Length;
//request.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
if (headers != null)
{
foreach (var v in headers)
{
if (v.Key is HttpRequestHeader)
request.Headers[(HttpRequestHeader)v.Key] = v.Value;
else
request.Headers[v.Key.ToString()] = v.Value;
}
}
HttpWebResponse response = null;
try
{
// Get the response.
response = (HttpWebResponse)request.GetResponse();
// Display the status.
Console.WriteLine(response.StatusDescription);
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Cleanup the streams and the response.
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
catch (Exception e)
{
return e.Message;
}
}
}
template_Id在小程序后台进行设置(功能-订阅消息-我的模板中进行添加)
这里代码只做参考 具体实现请不要使用静态类 并且access_token也需要缓存1-2小时 access_token过期时进行刷新