WebApi后端框架服务端框架开发微信小程序订阅消息接口
WebApi后端框架服务端框架开发微信小程序订阅消息接口
服务端业务逻辑处理:
微信小程序接受到消息:
PS:关于微信小程序是否支持极光推送信息?
推送消息是系统级别的,小程序包裹在微信里,微信又被包裹在系统中,目前是不支持的,除非微信推出相应的接口,那是不可能的!微信不会这么做,不能做,估计永远不会做,不然的话微信内部的一套消息通知就废了。
微信本身就是既时通讯社交工具,极光推送作为细分领域的竞争对手,怎么可能植入对手的功能到自己的系统呢?所以,我们只能采用微信自己的规则,就是微信小程序的通知方案,即服务通知或者模板消息。
微信小程序前端页面订阅消息弹窗确认,勾选
“总是保持以上选择,不再询问”,点击【允许】。
但是!!! 点击一次订阅,只能推送一次消息!!!然而系统需要频繁推送消息的!!!
为了解决这个问题,必须在微信小程序某个节点触发下一次订阅,形成一个无限订阅消息的循环,比如打开某个界面,触发某个事件开始订阅。
获取微信小程序token:
C# Code:
//获取微信token
string geturl = $"https://api.weixin.qq.com/cgi-bin/token&grant_type=client_credential&appid={WXAPP.appid}&secret={WXAPP.secret}";
string json = WebApiTools.Get(geturl, "");
ModelWXGetTokenResponse re = JsonConvertEx.ToObject<ModelWXGetTokenResponse>(json);
//获取微信token
string geturl = $"https://api.weixin.qq.com/cgi-bin/token&grant_type=client_credential&appid={WXAPP.appid}&secret={WXAPP.secret}";
string json = WebApiTools.Get(geturl, "");
ModelWXGetTokenResponse re = JsonConvertEx.ToObject<ModelWXGetTokenResponse>(json);
返回token模型:
C# Code:
public class ModelWXGetTokenResponse
{
public string access_token { get; set; }// string 获取到的凭证
public Int32 expires_in { get; set; }// 凭证有效时间,单位:秒。目前是7200秒之内的值。
public string errcode { get; set; }// number 错误码
public string errmsg { get; set; }// string 错误信息
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
public class ModelWXGetTokenResponse
{
public string access_token { get; set; }// string 获取到的凭证
public Int32 expires_in { get; set; }// 凭证有效时间,单位:秒。目前是7200秒之内的值。
public string errcode { get; set; }// number 错误码
public string errmsg { get; set; }// string 错误信息
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
服务端业务逻辑处理:
1. 根据微信小程序消息模板创建订阅消息相关对象实例,参数根据模板自定义,比如thing1,thing2。
2. 调用微信小程序消息订阅接口开始推送消息。
C# Code:
//消息模板内容
ModelWXSubscribeMessageTemplate data = new ModelWXSubscribeMessageTemplate
{
thing1 = new ModelWXSubscribeMessageParam { value = "【" + goods.CityFromName + "】-->【" + goods.CityToName + "】" },
thing2 = new ModelWXSubscribeMessageParam { value = goods.GoodsName + " " + goods.WV + " " + goods.Unit }
};
//调用微信后台,订阅消息接口的请求参数
ModelWXSubscribeMessage subscribe = new ModelWXSubscribeMessage
{
access_token = re.access_token,
data = data,
page = pageParam,
template_id = "nYSABmQjT2Dr1UyLXq5pUYSfPLob4Ejtig7ZtkxOkoo",
touser = goods.WXOpenId//openid
};
string dataMsg = JsonConvertEx.ToJson(subscribe);//转成JSON
//提交微信后台,URL
string posturl = $"https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={re.access_token}";
string result = WebApiTools.Post(posturl, dataMsg);
//转换对象
ModelWXPostResult postResult = JsonConvertEx.ToObject<ModelWXPostResult>(result);
if (postResult.errcode != 0) LogManager.Log.Write("PushWxMessage", "发布货源推送微信订阅消息失败!", goods.GoodsID);
//来源:C/S框架网(www.csframework.com) QQ:23404761
//消息模板内容
ModelWXSubscribeMessageTemplate data = new ModelWXSubscribeMessageTemplate
{
thing1 = new ModelWXSubscribeMessageParam { value = "【" + goods.CityFromName + "】-->【" + goods.CityToName + "】" },
thing2 = new ModelWXSubscribeMessageParam { value = goods.GoodsName + " " + goods.WV + " " + goods.Unit }
};
//调用微信后台,订阅消息接口的请求参数
ModelWXSubscribeMessage subscribe = new ModelWXSubscribeMessage
{
access_token = re.access_token,
data = data,
page = pageParam,
template_id = "nYSABmQjT2Dr1UyLXq5pUYSfPLob4Ejtig7ZtkxOkoo",
touser = goods.WXOpenId//openid
};
string dataMsg = JsonConvertEx.ToJson(subscribe);//转成JSON
//提交微信后台,URL
string posturl = $"https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={re.access_token}";
string result = WebApiTools.Post(posturl, dataMsg);
//转换对象
ModelWXPostResult postResult = JsonConvertEx.ToObject<ModelWXPostResult>(result);
if (postResult.errcode != 0) LogManager.Log.Write("PushWxMessage", "发布货源推送微信订阅消息失败!", goods.GoodsID);
//来源:C/S框架网(www.csframework.com) QQ:23404761
微信小程序消息订阅请求接口参数模型:
C# Code:
/// <summary>
/// 微信小程序消息订阅请求接口参数
/// </summary>
public class ModelWXSubscribeMessage
{
public string access_token { get; set; }// string 是 接口调用凭证
public string touser { get; set; }// string 是 接收者(用户)的 openid
public string template_id { get; set; }//string 是 所需下发的订阅模板id
public string page { get; set; }// string 否 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index? foo = bar)。该字段不填则模板无跳转。
public ModelWXSubscribeMessageTemplate data { get; set; }//Object 是 模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any} }
}
//data对象
public class ModelWXSubscribeMessageTemplate
{
public ModelWXSubscribeMessageParam thing1 { get; set; }
public ModelWXSubscribeMessageParam thing2 { get; set; }
}
//参数对象
public class ModelWXSubscribeMessageParam
{
public string value { get; set; }
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
/// <summary>
/// 微信小程序消息订阅请求接口参数
/// </summary>
public class ModelWXSubscribeMessage
{
public string access_token { get; set; }// string 是 接口调用凭证
public string touser { get; set; }// string 是 接收者(用户)的 openid
public string template_id { get; set; }//string 是 所需下发的订阅模板id
public string page { get; set; }// string 否 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index? foo = bar)。该字段不填则模板无跳转。
public ModelWXSubscribeMessageTemplate data { get; set; }//Object 是 模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any} }
}
//data对象
public class ModelWXSubscribeMessageTemplate
{
public ModelWXSubscribeMessageParam thing1 { get; set; }
public ModelWXSubscribeMessageParam thing2 { get; set; }
}
//参数对象
public class ModelWXSubscribeMessageParam
{
public string value { get; set; }
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
微信小程序订阅消息开发指南:
小程序订阅消息指南:
小程序订阅消息接口:
服务端调用API说明:
客户端测试程序:
C# Code:
ModelWXPushMessage model = new ModelWXPushMessage()
{
CityFrom = 440608,
CityTo = 420116,
CityFromName = "上海",
CityToName = "武汉",
GoodsID = "1100000022222",
GoodsName = "瓷砖胶+涂料",
Unit = "吨",
WV = 11,
WXOpenId = "oDKo4495z9v_FL0tm0rjND0vLl7k"//郑总
};
ModelRequestAction request = new ModelRequestAction();
request.Action = 68112; //接口
request.ApiKey = API_KEY; //密钥
request.Data = JsonConvert.SerializeObject(model);
request.Operation = "PushWxMessage";
request.Sign = CryptoHelper.ToMD5(API_KEY + request.Data + SECRET_KEY);//数字签名
string url = BASE_URL + "/api/do/post";//url地址
string param = JsonConvert.SerializeObject(request);//序列化实体对象
//post请求返回结果
string result = WebApiTools.Post(url, param, "application/json;charset=UTF-8");
//来源:C/S框架网(www.csframework.com) QQ:23404761
ModelWXPushMessage model = new ModelWXPushMessage()
{
CityFrom = 440608,
CityTo = 420116,
CityFromName = "上海",
CityToName = "武汉",
GoodsID = "1100000022222",
GoodsName = "瓷砖胶+涂料",
Unit = "吨",
WV = 11,
WXOpenId = "oDKo4495z9v_FL0tm0rjND0vLl7k"//郑总
};
ModelRequestAction request = new ModelRequestAction();
request.Action = 68112; //接口
request.ApiKey = API_KEY; //密钥
request.Data = JsonConvert.SerializeObject(model);
request.Operation = "PushWxMessage";
request.Sign = CryptoHelper.ToMD5(API_KEY + request.Data + SECRET_KEY);//数字签名
string url = BASE_URL + "/api/do/post";//url地址
string param = JsonConvert.SerializeObject(request);//序列化实体对象
//post请求返回结果
string result = WebApiTools.Post(url, param, "application/json;charset=UTF-8");
//来源:C/S框架网(www.csframework.com) QQ:23404761
若OpenID没有订阅消息,会报43101错误。
.NET WebApi开发框架|MVC框架|后端框架|服务端框架-标准版V1.0
适用开发:快速构建支持多种客户端的服务端程序,支持APP、B/S、C/S跨平台移动终端等。
运行平台:Windows + .NET Framework 4.5
开发工具:Visual Studio 2015+,C#语言
数据库:Microsoft SQLServer 2008R2+(支持多数据库:Oracle/MySql)
运行平台:Windows + .NET Framework 4.5
开发工具:Visual Studio 2015+,C#语言
数据库:Microsoft SQLServer 2008R2+(支持多数据库:Oracle/MySql)
扫一扫加微信:
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网