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
-
-
/// <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;
-
}
-
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
});
}
