C#—ASP.NET:集成极光推送(Push API v3)
1、极光推送官网(https://www.jpush.cn/)申请一个账号。
2、服务中心,开发者服务中,创建一个新的应用,输入正确的Android的包名
3、获取到了一个AppKey 和一个 Master Secret,这两个参数比较重要,验证权限使用。
4、去官网找到下载C# SDK的包https://docs.jiguang.cn/jpush/resources/
Github 源码:https://github.com/jpush/jpush-api-csharp-client
5、源码生成DLL
6、项目引用DLL,新建类 using Jiguang.JPush.Model;
7、代码封装HTTP 调用官方API,转载地址为pohreb博客:https://www.cnblogs.com/yangwujun/p/5973120.html
/// 极光推送的最新版:PUSH-API-V3
- #region 基础方法
- /// <summary>
- /// 应用标识:极光推送的用户名
- /// </summary>
- private const string AppKey = "填写你应用的AppKey";
- /// <summary>
- /// 极光推送的密码
- /// </summary>
- private const string MasterSecret = "填写你的MasterSecret";
- ///// <summary>
- ///// 极光推送请求的url地址
- ///// </summary>
- private const string RequestUrl = "https://api.jpush.cn/v3/push";
- //private const string RequestUrl = "https://bjapi.push.jiguang.cn/v3/push";
- /// <summary>
- /// 查询推送结果请求的Url地址
- /// </summary>
- private const string ReceivedUrl = "https://report.jpush.cn/v3/received";
- /// <summary>
- /// 发送推送请求到JPush,使用HttpWebRequest
- /// </summary>
- /// <param name="method">传入POST或GET</param>
- /// <param name="url">固定地址</param>
- /// <param name="auth">用户名AppKey和密码MasterSecret形成的Base64字符串</param>
- /// <param name="reqParams">请求的json参数,一般由Platform(平台)、Audience(设备对象标识)、Notification(通知)、Message(自定义消息)、Options(推送可选项)组成</param>
- /// <returns></returns>
- private static string SendRequest(String method, String url, String auth, String reqParams)
- {
- string resultJson = "";
- HttpWebRequest myReq = null;
- HttpWebResponse response = null;
- try
- {
- myReq = (HttpWebRequest)WebRequest.Create(url);
- myReq.Method = method;
- myReq.ContentType = "application/json";
- if (!String.IsNullOrEmpty(auth))
- {
- myReq.Headers.Add("Authorization", "Basic " + auth);
- }
- if (method == "POST")
- {
- byte[] bs = UTF8Encoding.UTF8.GetBytes(reqParams);
- myReq.ContentLength = bs.Length;
- using (Stream reqStream = myReq.GetRequestStream())
- {
- reqStream.Write(bs, 0, bs.Length);
- reqStream.Close();
- }
- }
- response = (HttpWebResponse)myReq.GetResponse();
- HttpStatusCode statusCode = response.StatusCode;
- if (Equals(response.StatusCode, HttpStatusCode.OK))
- {
- using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8))
- {
- resultJson = reader.ReadToEnd();
- try
- {
- object json = Newtonsoft.Json.JsonConvert.DeserializeObject(resultJson);
- }
- catch
- {
- resultJson = string.Format("{{\"error\": {{\"message\": \"{0}\", \"code\": 10086}}}}", "响应的结果不是正确的json格式");
- }
- }
- }
- }
- catch (WebException ex)
- {
- if (ex.Status == WebExceptionStatus.ProtocolError)
- {
- HttpStatusCode errorCode = ((HttpWebResponse)ex.Response).StatusCode;
- string statusDescription = ((HttpWebResponse)ex.Response).StatusDescription;
- using (StreamReader sr = new StreamReader(((HttpWebResponse)ex.Response).GetResponseStream(), System.Text.Encoding.UTF8))
- {
- resultJson = sr.ReadToEnd();
- //{"errcode":404,"errmsg":"request api doesn't exist"}
- Dictionary<string, object> dict = JsonToDictionary(resultJson);
- string errCode = "10086";
- string errMsg = "发送推送的请求地址不存在或无法连接";
- if (dict.ContainsKey("errcode"))
- {
- errCode = dict["errcode"].ToString();
- }
- if (dict.ContainsKey("errmsg"))
- {
- errMsg = dict["errmsg"].ToString();
- }
- resultJson = string.Format("{{\"error\": {{\"message\": \"{0}\", \"code\": {1}}}}}", errMsg, errCode);
- }
- }
- else
- {
- //这里一定是error作为键名(自定义错误号10086),和极光推送失败时的json格式保持一致 如 {"error": {"message": "Missing parameter", "code": 1002}}
- resultJson = string.Format("{{\"error\": {{\"message\": \"{0}\", \"code\": 10086}}}}", ex.Message.Replace("\"", " ").Replace("'", " "));
- }
- }
- catch (System.Exception ex)
- {
- resultJson = string.Format("{{\"error\": {{\"message\": \"{0}\", \"code\": 10086}}}}", ex.Message.Replace("\"", " ").Replace("'", " "));
- }
- finally
- {
- if (response != null)
- {
- response.Close();
- }
- if (myReq != null)
- {
- myReq.Abort();
- }
- }
- return resultJson;
- }
- /// <summary>
- /// 通过用户名AppKey和密码获取验证码
- /// </summary>
- /// <returns></returns>
- private static string GetBase64Auth()
- {
- string str = AppKey + ":" + MasterSecret;
- byte[] bytes = Encoding.Default.GetBytes(str);
- return Convert.ToBase64String(bytes);
- }
- /// <summary>
- /// 发送推送请求到JPush
- /// </summary>
- /// <param name="method">POST或GET</param>
- /// <param name="reqParams">请求的json参数,一般由Platform(平台)、Audience(设备对象标识)、Notification(通知)、Message(自定义消息)、Options(推送可选项)组成</param>
- /// <returns></returns>
- public static string SendRequest(String method, String reqParams)
- {
- string auth = GetBase64Auth();
- return SendRequest(method, RequestUrl, auth, reqParams);
- }
- /// <summary>
- /// 发送Post请求
- /// </summary>
- /// <param name="reqParams">请求的json参数,一般由Platform(平台)、Audience(设备对象标识)、Notification(通知)、Message(自定义消息)、Options(推送可选项)组成</param>
- /// <returns></returns>
- public static string SendPostRequest(String reqParams)
- {
- string auth = GetBase64Auth();
- return SendRequest("POST", RequestUrl, auth, reqParams);
- }
- /// <summary>
- /// 发送Get请求
- /// </summary>
- /// <param name="reqParams">请求的json参数,一般由Platform(平台)、Audience(设备对象标识)、Notification(通知)、Message(自定义消息)、Options(推送可选项)组成</param>
- /// <returns></returns>
- public static string SendGetRequest(String reqParams)
- {
- string auth = GetBase64Auth();
- return SendRequest("GET", RequestUrl, auth, reqParams);
- }
- /*
- * 生成唯一的sendNo的方法: 取序列号
- * 查看返回结果的方法
- */
- /// <summary>
- /// 查询推送的结果
- /// </summary>
- /// <param name="msg_ids">生成的json信息唯一id</param>
- /// <returns></returns>
- public static string GetReceivedResult(String msg_ids)
- {
- string url = ReceivedUrl + "?msg_ids=" + msg_ids;
- String auth = GetBase64Auth();
- return SendRequest("GET", url, auth, null);
- }
- /*
- * 1.正确时返回结果{"sendno":"123456","msg_id":"1799597405"}
- * 或者 {"sendno":"0","msg_id":"351403900"}
- * 2.入参json完全正确,但找不到要到达的设备。错误时:返回
- * {"msg_id": 3125719446, "error": {"message": "cannot find user by this audience", "code": 1011}}
- * 3.传入空字符串 或者 非json格式,或者没有必须的选项:{"error": {"message": "Missing parameter", "code": 1002}}
- * 传入的键(键区分大小写)、值不符合要求 {"error": {"message": "Audience value must be JSON Array format!", "code": 1003}}
- */
- /// <summary>
- /// 将返回的json转换为Hashtable对象
- /// </summary>
- /// <param name="jsonString"></param>
- /// <returns></returns>
- public static Hashtable JsonToHashtable(string jsonString)
- {
- /*
- * 正确时返回结果{"sendno":"123456","msg_id":"1799597405"}
- * {"sendno":"0","msg_id":"351403900"}
- * 入参json完全正确,但找不到要到达的设备。错误时:返回 {"msg_id": 3125719446, "error": {"message": "cannot find user by this audience", "code": 1011}}
- * 传入空字符串 或者 非json格式,或者没有必须的选项:{"error": {"message": "Missing parameter", "code": 1002}}
- * 传入的键值不符合要求 {"error": {"message": "Audience value must be JSON Array format!", "code": 1003}} 键区分大小写
- */
- Hashtable ht = new Hashtable();
- object json = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString);
- //返回的结果一定是一个json对象
- Newtonsoft.Json.Linq.JObject jsonObject = json as Newtonsoft.Json.Linq.JObject;
- if (jsonObject == null)
- {
- return ht;
- }
- foreach (Newtonsoft.Json.Linq.JProperty jProperty in jsonObject.Properties())
- {
- Newtonsoft.Json.Linq.JToken jToken = jProperty.Value;
- string value = "";
- if (jToken != null)
- {
- value = jToken.ToString();
- }
- ht.Add(jProperty.Name, value);
- }
- return ht;
- }
- /// <summary>
- /// 根据json返回的结果判断是否推送成功
- /// </summary>
- /// <param name="jsonString">响应的json</param>
- /// <param name="errorMessage">错误信息</param>
- /// <param name="errorCode">错误号</param>
- /// <returns></returns>
- public static bool IsSuccess(string jsonString, out string errorMessage, out string errorCode)
- {
- Hashtable ht = JsonToHashtable(jsonString);
- errorMessage = "";
- errorCode = "";
- foreach (string key in ht.Keys)
- {
- //如果存在error键,说明推送出错
- if (key == "error")
- {
- string errJson = ht[key].ToString();
- Hashtable htError = JsonToHashtable(errJson);
- errorMessage = htError["message"].ToString();
- errorCode = htError["code"].ToString();
- return false;
- }
- }
- return true;
- }
- /// <summary>
- /// 根据返回的响应json来判断推送是否成功,成功时记录sendno与msg_id。
- /// 失败时记录错误信息errorMessage、错误号errCode等
- /// </summary>
- /// <param name="jsonString">响应的json</param>
- /// <param name="errorMessage">错误信息</param>
- /// <param name="errorCode">错误号</param>
- /// <param name="sendno">用户自定义的推送编号(从序列号中获取),不设置则为0,成功后返回该编号</param>
- /// <param name="msg_id">极光服务器处理后返回的信息编号</param>
- /// <returns></returns>
- public static bool IsSuccess(string jsonString, out string errorMessage, out string errorCode, out string sendno, out string msg_id)
- {
- bool result = IsSuccess(jsonString, out errorMessage, out errorCode);
- Hashtable ht = JsonToHashtable(jsonString);
- sendno = "";
- msg_id = "";
- if (result) //推送成功时,只有键sendno、msg_id
- {
- sendno = ht["sendno"].ToString();
- msg_id = ht["msg_id"].ToString();
- }
- else //如果失败时存在msg_id键,则记录msg_id的值
- {
- if (ht.ContainsKey("msg_id"))
- {
- msg_id = ht["msg_id"].ToString();
- }
- }
- return result;
- }
- /// <summary>
- /// 将返回的json转换为字典Dictionary对象
- /// </summary>
- /// <param name="jsonString"></param>
- /// <returns></returns>
- public static Dictionary<string, object> JsonToDictionary(string jsonString)
- {
- Dictionary<string, object> ht = new Dictionary<string, object>();
- object json = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString);
- //返回的结果一定是一个json对象
- Newtonsoft.Json.Linq.JObject jsonObject = json as Newtonsoft.Json.Linq.JObject;
- if (jsonObject == null)
- {
- return ht;
- }
- foreach (Newtonsoft.Json.Linq.JProperty jProperty in jsonObject.Properties())
- {
- Newtonsoft.Json.Linq.JToken jToken = jProperty.Value;
- string value = "";
- if (jToken != null)
- {
- value = jToken.ToString();
- }
- ht.Add(jProperty.Name, value);
- }
- return ht;
- }
- #endregion
8、其中我们主要使用registration_id的方式推送,也就是通过APP上的用户Token推送。
新增两个实体类一个存储APP用户Token,以及设备平台(安卓,苹果),一个存储推送记录,其实极光官网也有推送记录。
因为需要推送多个用户情况,新添List存储registration_id
新建返回对象存储返回信息
扩展方法使用,根据文档实现JSON格式并调用基本方法实现推送https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/
/// <summary>
- /// </summary>
- /// <param name="RegistrationIDList">推送TokenID集合</param>
- /// <param name="title">标题</param>
- /// <param name="senduser">作者</param>
- /// <param name="toid">推送对象ID</param>
- /// <param name="contype">推送对象</param>
- /// <param name="dataid"></param>
- /// <param name="strMsg">推送内容</param>
- /// <param name="is_production"></param>
- /// <param name="strLog">返回日志</param>
- /// <returns></returns>
- public static bool SendPushV2(List<string> RegistrationIDList, string title, string senduser, string toid,int contype,string dataid, string strMsg, bool is_production, out string strLog)
- {
- try
- {
- var parmARR = new Dictionary<string, object>();
- parmARR.Add("dataid", dataid);
- var mm = new M_PushRegistration();
- mm.registration_id = RegistrationIDList;
- //生成JSON格式参数
- PushPayload pushPayload = new PushPayload()
- {
- Platform = new List<string> { "android", "ios" },//推送平台
- Audience = mm,//推送对象
- Notification = new Notification
- {
- Alert = strMsg,
- Android = new Android
- {
- Alert = strMsg,
- Title = "你的标题",
- Extras = parmARR
- },
- IOS = new IOS
- {
- Alert = strMsg,
- Badge = "+1",
- Extras = parmARR
- }
- },
- Options = new Options
- {
- IsApnsProduction = true // 设置 iOS 推送生产环境。不设置默认为开发环境。
- }
- };
- var strParms = pushPayload.Exp_ModelToJson();
- strParms.WriteFile("log/push");
- var result = JPushHelperV3.SendPostRequest(strParms);
- var retM = result.Exp_JsonToModel<M_PushReturn>(1);
- strLog = "";
- strLog += "result=" + result + "&&retModel=" + retM.Exp_ModelToJson();
- strLog.WriteFile("log/push");
- if (retM.error == null)
- {
- //保存推送信息
- string[] teacherArr = toid.Split(',');
- for (int i = 0; i < teacherArr.Length; i++)
- {
- D_T_PushMsg_Exp pushmsgDal = new D_T_PushMsg_Exp();
- M_T_PushMsg pushmsgModel = new M_T_PushMsg();
- pushmsgModel.Title = title;
- pushmsgModel.MsgAuthor = senduser;
- pushmsgModel.MsgContent = strMsg;
- pushmsgModel.Flag = true;
- pushmsgModel.IsRead = false;
- pushmsgModel.IsSend = true;
- pushmsgModel.Contype = contype;
- pushmsgModel.Remark1 = teacherArr[i].Exp_IntTryParse(); //发送给谁
- pushmsgModel.AddTime = DateTime.Now;
- pushmsgModel.SendTime = DateTime.Now;
- pushmsgModel.Remark2 = "";
- pushmsgModel.Remark3 = false;
- pushmsgDal.Admin_Add(pushmsgModel);
- }
- strLog = "向设备推送消息成功\r\n请求参数=" + strParms + "\r\n";
- return true;
- }
- else
- {
- strLog = "推送失败,错误码:" + retM.error.code + ",错误信息:" + retM.error.message;
- return false;
- }
- }
- catch (Exception ex)
- {
- strLog = "推送异常:" + ex.Message;
- ex.Exp_WriteErrorLog();
- return false;
- }
- }
调用该方法即可实现推送。
9、调试,注意事项。
需改进:该基本方法,返回的错误信息不知什么原因并不是官方给出的错误信息,他判断的是HTTP Status Code 返回的异常,容易迷惑以为授权失败。具体错误信息可以去官网查看。或者使用https://api.jpush.cn/v3/push/validate校验API
扩展方法中的Exp_、Admin_方法为自己封装DLL功能,可根据要求自行编写,只是记录思路。
开发者服务中-应用设置-推送设置,IOS需要你填写授权方式,Android下可以快速集成扫描下载安装包。
https://docs.jiguang.cn/jpush/client/Android/android_3m/
安装应用并运行,点击 即可看见分配给本机的RegId
测试接口中可以填写该RegId,进行测试。需要手机能联网,部分机型可能收不到推送(努比亚部分机型?)
调用测试后可以在极光开发者服务-推送-推送历史中查看API类型的推送。
该RegId需要为极光分配给该应用下的该用户设备RegId,所以APP端也需要集成极光推送注册用户为极光推送对象,如果不是原生的开发,看你的需要选择:https://docs.jiguang.cn/jpush/client/client_plugins/
以上为接口端集成。
扩展记录——APICloud客户端集成极光推送(并点击推送消息跳转相应页面)
1、APICloud开发APP应用中端开发需要添加极光推送模块
2、APP应用中config.xml,加入配置app_key对应的APPKEY
3、 在应用打开时Index.html页面中加入注册RegId。
setToken=function() {
- //console.log('jpush:start==>token=' + token);
- var jpush = api.require('ajpush');//加载jpush模块
- if ('ios' == api.systemType) {
- jpush.getRegistrationId(function(ret) {
- //console.log('ios jpush getRegistrationId:' + JSON.stringify(ret));
- var registrationId = ret.id;
- $api.setStorage('user.token', ret.id);
- });
- } else {
- jpush.init(function(ret0, err0) {//安卓需要初始化jpush
- // console.log('android jpush.init:' + JSON.stringify(ret0)+"|||||"+JSON.stringify(err0));
- if (ret0.status == 1) {
- jpush.getRegistrationId(function(ret) {//获取极光该应用该设备下的Regid
- // console.log('android jpush getRegistrationId:' + JSON.stringify(ret));
- var registrationId = ret.id;//保存Regid
- $api.setStorage('user.token', ret.id);//保存Regid到用户信息,保存推送对象
- });
- }
- });
- }
- }
4、登陆时需要将该用户的(Regid)Token,更新到服务器端用户对应的推送下,也就是上面集成C#中的M_T_PushToken数据表中,方便推送时在里面寻找对象。
扩展更新——APICloud客户端推送,传递自定义参数,用来点击通知跳转页面。
1、主要是更改发送的参数配置即可,更新c#集成的SendPushV2方法中的部分代码,极光推送文档https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#notification
PushPayload pushPayload = new PushPayload()
Audience = mm,
Notification = new Notification
{
Alert = strMsg,
Android = new Android
{
Alert = strMsg,//内容
Title = title,//标题
Style = 1,//样式
BigText = strMsg,//大文本
Extras = parmARR
},
IOS = new IOS
{
Alert = strMsg,
Badge = "+1",
Extras = parmARR
}
},
Options = new Options
{
IsApnsProduction = true // 设置 iOS 推送生产环境。不设置默认为开发环境。
}
};
2、中的Extras数据应为JSON格式数据,c#中 var parmARR = new Dictionary<string, object>();方法实现
3、检查接口端推送数据格式是否正确后,可以登陆极光官网,(极光开发者服务-相应应用推送-发送通知)中模拟推送,选择需要的通知样式。选择大段文本样式,可以解决推送内容过多显示不全情况。
4、接口端配置完成后,需要在客户端监听通知点击事件。注意点击后的跳转地址即可。
var jpush = api.require('ajpush');
//安卓端监听
api.addEventListener({name:'appintent'}, function(ret,err) {
// alert('通知被点击,收到数据:\n' + JSON.stringify(ret));//监听通知被点击后收到的数据
var data=ret.appParam.ajpush.extra;
openWinExp(data.DWin,data.Win,{"TypeID":data.TypeID});
})
//IOS端监听
api.addEventListener({name:'noticeclicked'}, function(ret,err) {
// alert('通知被点击,收到数据:\n' + JSON.stringify(ret));//监听通知被点击后收到的数据
var data=ret.appParam.ajpush.extra;
openWinExp(data.DWin,data.Win,{"TypeID":data.TypeID});
})
//因为在Index下,所以注意跳转地址。
openWinExp=function(dir,name, parms) {
//console.log("openWin===>dir=" + dir + "&name=" + name + "&parms=" + JSON.stringify(parms));
api.openWin({
name: name,
url: strurl='html/'+ dir + '/' + name + '.html',//注意跳转地址
bounces: false,
rect: {
x: 0,
y: 0,
w: 'auto',
h: 'auto'
},
delay: 0,
reload: true,
slidBackEnabled: false,
animation: {
type: stnet.setting.animation.type,
duration: 300,
subType: (parms && parms.type) ? 'from_' + parms.type : stnet.setting.animation.subType
},
pageParam: parms
});
}