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
/// 参考地址 http://docs.jpush.cn/display/dev/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();
-
}
-