一、极光推送官网(https://www.jpush.cn/)申请一个帐号。html
二、服务中心,开发者服务中,建立一个新的应用,输入正确的Android的包名android

三、获取到了一个AppKey 和一个 Master Secret,这两个参数比较重要,验证权限使用。ios
四、去官网找到下载C# SDK的包https://docs.jiguang.cn/jpush/resources/git

Github 源码:https://github.com/jpush/jpush-api-csharp-clientgithub
五、源码生成DLLjson

六、项目引用DLL,新建类 using Jiguang.JPush.Model;c#
七、代码封装HTTP 调用官方API,转载地址为pohreb博客:http://www.javashuo.com/article/p-dsjbleec-cb.htmlapi
服务器
-
-
-
-
-
private const string AppKey = "填写你应用的AppKey";
-
-
-
-
private const string MasterSecret = "填写你的MasterSecret";
-
-
-
-
private const string RequestUrl = "https://api.jpush.cn/v3/push";
-
-
-
-
-
private const string ReceivedUrl = "https://report.jpush.cn/v3/received";
-
-
-
-
-
-
-
-
-
private static string SendRequest(String method, String url, String auth, String reqParams)
-
-
-
HttpWebRequest myReq =
null;
-
HttpWebResponse response =
null;
-
-
-
myReq = (HttpWebRequest)WebRequest.Create(url);
-
-
myReq.ContentType =
"application/json";
-
if (!String.IsNullOrEmpty(auth))
-
-
myReq.Headers.Add(
"Authorization", "Basic " + auth);
-
-
-
-
byte[] bs = UTF8Encoding.UTF8.GetBytes(reqParams);
-
myReq.ContentLength = bs.Length;
-
using (Stream reqStream = myReq.GetRequestStream())
-
-
reqStream.Write(bs,
0, bs.Length);
-
-
-
-
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();
-
-
-
object json = Newtonsoft.Json.JsonConvert.DeserializeObject(resultJson);
-
-
-
-
resultJson =
string.Format("{{\"error\": {{\"message\": \"{0}\", \"code\": 10086}}}}", "响应的结果不是正确的json格式");
-
-
-
-
-
-
-
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();
-
-
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);
-
-
-
-
-
-
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("'", " "));
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
private static string GetBase64Auth()
-
-
string str = AppKey + ":" + MasterSecret;
-
byte[] bytes = Encoding.Default.GetBytes(str);
-
return Convert.ToBase64String(bytes);
-
-
-
-
-
-
-
-
public static string SendRequest(String method, String reqParams)
-
-
string auth = GetBase64Auth();
-
return SendRequest(method, RequestUrl, auth, reqParams);
-
-
-
-
-
-
-
public static string SendPostRequest(String reqParams)
-
-
string auth = GetBase64Auth();
-
return SendRequest("POST", RequestUrl, auth, reqParams);
-
-
-
-
-
-
-
public static string SendGetRequest(String reqParams)
-
-
string auth = GetBase64Auth();
-
return SendRequest("GET", RequestUrl, auth, reqParams);
-
-
-
-
-
-
-
-
-
-
-
public static string GetReceivedResult(String msg_ids)
-
-
string url = ReceivedUrl + "?msg_ids=" + msg_ids;
-
String auth = GetBase64Auth();
-
return SendRequest("GET", url, auth, null);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
public static Hashtable JsonToHashtable(string jsonString)
-
-
-
-
-
-
-
-
-
Hashtable ht =
new Hashtable();
-
object json = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString);
-
-
Newtonsoft.Json.Linq.JObject jsonObject = json
as Newtonsoft.Json.Linq.JObject;
-
-
-
-
-
foreach (Newtonsoft.Json.Linq.JProperty jProperty in jsonObject.Properties())
-
-
Newtonsoft.Json.Linq.JToken jToken = jProperty.Value;
-
-
-
-
value = jToken.ToString();
-
-
ht.Add(jProperty.Name,
value);
-
-
-
-
-
-
-
-
-
-
-
public static bool IsSuccess(string jsonString, out string errorMessage, out string errorCode)
-
-
Hashtable ht = JsonToHashtable(jsonString);
-
-
-
foreach (string key in ht.Keys)
-
-
-
-
-
string errJson = ht[key].ToString();
-
Hashtable htError = JsonToHashtable(errJson);
-
errorMessage = htError[
"message"].ToString();
-
errorCode = htError[
"code"].ToString();
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
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 = ht[
"sendno"].ToString();
-
msg_id = ht[
"msg_id"].ToString();
-
-
-
-
if (ht.ContainsKey("msg_id"))
-
-
msg_id = ht[
"msg_id"].ToString();
-
-
-
-
-
-
-
-
-
-
public static Dictionary<string, object> JsonToDictionary(string jsonString)
-
-
Dictionary<
string, object> ht = new Dictionary<string, object>();
-
object json = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString);
-
-
Newtonsoft.Json.Linq.JObject jsonObject = json
as Newtonsoft.Json.Linq.JObject;
-
-
-
-
-
foreach (Newtonsoft.Json.Linq.JProperty jProperty in jsonObject.Properties())
-
-
Newtonsoft.Json.Linq.JToken jToken = jProperty.Value;
-
-
-
-
value = jToken.ToString();
-
-
ht.Add(jProperty.Name,
value);
-
-
-
-
八、其中咱们主要使用registration_id的方式推送,也就是经过APP上的用户Token推送。app
新增两个实体类一个存储APP用户Token,以及设备平台(安卓,苹果),一个存储推送记录,其实极光官网也有推送记录。

由于须要推送多个用户状况,新添List存储registration_id

新建返回对象存储返回信息

扩展方法使用,根据文档实现JSON格式并调用基本方法实现推送https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/
-
-
-
-
-
-
-
-
-
-
-
-
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)
-
-
-
-
var parmARR = new Dictionary<string, object>();
-
parmARR.Add(
"dataid", dataid);
-
var mm = new M_PushRegistration();
-
mm.registration_id = RegistrationIDList;
-
-
PushPayload pushPayload =
new PushPayload()
-
-
Platform =
new List<string> { "android", "ios" },
-
-
Notification =
new Notification
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
var strParms = pushPayload.Exp_ModelToJson();
-
strParms.WriteFile(
"log/push");
-
var result = JPushHelperV3.SendPostRequest(strParms);
-
var retM = result.Exp_JsonToModel<M_PushReturn>(1);
-
-
strLog +=
"result=" + result + "&&retModel=" + retM.Exp_ModelToJson();
-
strLog.WriteFile(
"log/push");
-
-
-
-
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";
-
-
-
-
-
strLog =
"推送失败,错误码:" + retM.error.code + ",错误信息:" + retM.error.message;
-
-
-
-
-
-
strLog =
"推送异常:" + ex.Message;
-
-
-
-
调用该方法便可实现推送。
九、调试,注意事项。
需改进:该基本方法,返回的错误信息不知什么缘由并非官方给出的错误信息,他判断的是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客户端集成极光推送(并点击推送消息跳转相应页面)
一、APICloud开发APP应用中端开发须要添加极光推送模块

二、APP应用中config.xml,加入配置app_key对应的APPKEY

三、 在应用打开时Index.html页面中加入注册RegId。
setToken=function() {
var token = $api.getStorage('user.token');
-
-
var jpush = api.require('ajpush');
-
if ('ios' == api.systemType) {
-
jpush.getRegistrationId(
function(ret) {
-
-
var registrationId = ret.id;
-
$api.setStorage(
'user.token', ret.id);
-
-
-
jpush.init(
function(ret0, err0) {
-
-
-
jpush.getRegistrationId(
function(ret) {
-
-
var registrationId = ret.id;
-
$api.setStorage(
'user.token', ret.id);
-
-
-
-
-
四、登录时须要将该用户的(Regid)Token,更新到服务器端用户对应的推送下,也就是上面集成C#中的M_T_PushToken数据表中,方便推送时在里面寻找对象。
扩展更新——APICloud客户端推送,传递自定义参数,用来点击通知跳转页面。
一、主要是更改发送的参数配置便可,更新c#集成的SendPushV2方法中的部分代码,极光推送文档https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#notification
PushPayload pushPayload = new PushPayload()
Platform =
new List<string> { "android", "ios" },
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
}
};
二、中的Extras数据应为JSON格式数据,c#中 var parmARR = new Dictionary<string, object>();方法实现

三、检查接口端推送数据格式是否正确后,能够登录极光官网,(极光开发者服务-相应应用推送-发送通知)中模拟推送,选择须要的通知样式。选择大段文本样式,能够解决推送内容过多显示不全状况。

四、接口端配置完成后,须要在客户端监听通知点击事件。注意点击后的跳转地址便可。
var jpush = api.require('ajpush');
api.addEventListener({name:'appintent'}, function(ret,err) {
var data=ret.appParam.ajpush.extra;
openWinExp(data.DWin,data.Win,{"TypeID":data.TypeID});
})
api.addEventListener({name:'noticeclicked'}, function(ret,err) {
var data=ret.appParam.ajpush.extra;
openWinExp(data.DWin,data.Win,{"TypeID":data.TypeID});
})
openWinExp=function(dir,name, 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
});
}