使用极光推送(https://www.doczj.com/doc/d316483029.html,)向安卓手机推送消息【服务端向客
户端主送推送】,C#语言
在VisualStudio2010中新建网站JPushAndroid。添加引用json帮助类库Newtonsoft.Json.dll。在web.config增加appkey和mastersecret,可以在极光官网https://www.doczj.com/doc/d316483029.html,申请。web.config源码:
添加类JPushV3,会弹出保存类在App_code文件夹下,确定。
JpushV3代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using https://www.doczj.com/doc/d316483029.html,;
using System.Text;
using System.IO;
using System.Configuration;
using System.Collections;
/*
* 参考文档:https://www.doczj.com/doc/d316483029.html,/display/dev/Index 选择里面的:服务器端API,Push-API-V3
*
* 极光推送的网站的网址是:https://https://www.doczj.com/doc/d316483029.html,/
* 旧版本V2 https://www.doczj.com/doc/d316483029.html,/display/dev/Push+API+v2
* 最新版本V3 https://www.doczj.com/doc/d316483029.html,/display/dev/Push-API-v3
*
* 其中服务端的接口以及示例代码都在这里:https://www.doczj.com/doc/d316483029.html,/display/dev/Server-SDKs
*/
///
///极光推送的最新版:PUSH-API-V3
///参考地址https://www.doczj.com/doc/d316483029.html,/display/dev/Push-API-v3
/// POST https://https://www.doczj.com/doc/d316483029.html,/v3/push
///
public class JPushV3
{
///
///应用标识:极光推送的用户名
///
private readonly string AppKey = ConfigurationManager.AppSettings["AppKey"];
///
///极光推送的密码
///
private readonly string MasterSecret = ConfigurationManager.AppSettings["MasterSecret"];
///
///极光推送请求的url地址
///
private readonly string RequestUrl = "https://https://www.doczj.com/doc/d316483029.html,/v3/push";
///
///查询推送结果请求的Url地址
///
private readonly string ReceivedUrl = "https://https://www.doczj.com/doc/d316483029.html,/v3/received";
///
///发送推送请求到JPush,使用HttpWebRequest
///
///传入POST或GET
///固定地址
///用户名AppKey和密码MasterSecret形成的Base64字符串
///请求的json参数,一般由Platform(平台)、Audience(设备对象标识)、Notification(通知)、Message(自定义消息)、Options(推送可选项)组成
///
public 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 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;
}
///
///通过用户名AppKey和密码获取验证码
///
///
private string GetBase64Auth()
{
string str = this.AppKey + ":" + this.MasterSecret;
byte[] bytes = Encoding.Default.GetBytes(str);
return Convert.ToBase64String(bytes);
}
///
///发送推送请求到JPush
///
///POST或GET
///请求的json参数,一般由Platform(平台)、Audience(设备对象标识)、Notification(通知)、Message(自定义消息)、Options(推送可选项)组成
///
public string SendRequest(String method, String reqParams)
{
string auth = GetBase64Auth();
return SendRequest(method, this.RequestUrl, auth, reqParams);
}
///
///发送Post请求
///
///请求的json参数,一般由Platform(平台)、Audience(设备对象标识)、Notification(通知)、Message(自定义消息)、Options(推送可选项)组成
///
public string SendPostRequest(String reqParams)
{
string auth = GetBase64Auth();
return SendRequest("POST", this.RequestUrl, auth, reqParams);
}
///
///发送Get请求
///
///请求的json参数,一般由Platform(平台)、Audience(设备对象标识)、
Notification(通知)、Message(自定义消息)、Options(推送可选项)组成
///
public string SendGetRequest(String reqParams)
{
string auth = GetBase64Auth();
return SendRequest("GET", this.RequestUrl, auth, reqParams);
}
/*
* 生成唯一的sendNo的方法:取序列号
* 查看返回结果的方法
*/
///
///查询推送的结果
///
///生成的json信息唯一id
///
public string GetReceivedResult(String msg_ids)
{
string url = this.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}}
*/
///
///将返回的json转换为Hashtable对象
///
///
///
public 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(https://www.doczj.com/doc/d316483029.html,, value);
}
return ht;
}
///
///根据json返回的结果判断是否推送成功
///
///响应的json
///错误信息
///错误号
///
public 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;
}
///
///根据返回的响应json来判断推送是否成功,成功时记录sendno与msg_id。
///失败时记录错误信息errorMessage、错误号errCode等
///
///响应的json
///错误信息
///错误号
///用户自定义的推送编号(从序列号中获取),不设置则为0,成功后返回该编号
///极光服务器处理后返回的信息编号
///
public 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;
}
///
///将返回的json转换为字典Dictionary对象
///
///
///
public Dictionary
{
Dictionary
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(https://www.doczj.com/doc/d316483029.html,, value);
}
return ht;
}
}
☆JPushV2类代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Text;
using https://www.doczj.com/doc/d316483029.html,;
using System.IO;
using System.Security.Cryptography;
/*
* 参考文档:https://www.doczj.com/doc/d316483029.html,/display/dev/Index 选择里面的:服务器端API,Push-API-V3
*
* 极光推送的网站的网址是:https://https://www.doczj.com/doc/d316483029.html,/
* 旧版本V2 https://www.doczj.com/doc/d316483029.html,/display/dev/Push+API+v2
* 最新版本V3 https://www.doczj.com/doc/d316483029.html,/display/dev/Push-API-v3
*
* 其中服务端的接口以及示例代码都在这里:https://www.doczj.com/doc/d316483029.html,/display/dev/Server-SDKs
*/
///
///这个版本是Push-API-v2版本,只能用到2015-12-31,建议直接使用最新版v3
/// post处理地址https://www.doczj.com/doc/d316483029.html,:8800/v2/push
///
public class JPushV2
{
///
/// Android ApiKey
///
private readonly string AppKey = ConfigurationManager.AppSettings["AppKey"];
///
/// Android密码
///
private readonly string APIMasterSecret = ConfigurationManager.AppSettings["MasterSecret"];
///
/// Android极光推送:开始推送方法
///
///设备号RV1D41L5F1T
public void PushAndroid(string RegistrationID)
{
try
{
Random ran = new Random();
int sendno = ran.Next(1, 2100000000);//随机生成的一个编号
string app_key = AppKey;
string masterSecret = APIMasterSecret;
int receiver_type = 4;//接收者类型。2、指定的tag。3、指定的alias。4、广播:对app_key 下的所有用户推送消息。5、根据RegistrationID 进行推送。当前只是Android SDK r1.6.0 版本支持string receiver_value = RegistrationID;
/*
int msg_type = 1;//1、通知2、自定义消息(只有Android 支持)
//通过json来推送消息内容。title-标题content-推送的内容
string Title = "要推送的标题";
string Content = "要推送的内容";
string msg_content = "{\"n_builder_id\":\"00\",\"n_title\":\"" + Title + "\",\"n_content\":\"" + Content + "\"}";//消息内容
*/
/*
当调用参数msg_type = 1(通知)时,msg_content JSON 要求:
Key名称是否必须Value内容说明
n_builder_id 可选1-1000的数值,不填则默认为0,使用极光Push SDK 的默认通知样式。只有Android 支持这个参数。
n_title 可选通知标题。不填则默认使用该应用的名称。只有Android 支持这个参数。
n_content 必须通知内容。
n_extras 可选通知附加参数。JSON格式。客户端可取得全部内容。
*/
int msg_type = 2;//1、通知2、自定义消息(只有Android 支持)
//通过json来推送消息内容。title-标题content-推送的内容
string msg_content = "{\"message\":\"xxxx12344554\"}";//消息内容
/*
当调用参数msg_type = 2(自定义消息) 时,msg_content JSON 要求:
Key名称是否必须Value内容说明
message 必须自定义消息的内容。
content_type 可选message 字段里的内容类型。用于特定的message 内容解析
title 可选消息标题
extras 可选原样返回,JSON 格式的更多的附属信息
*/
string platform = "android";//目标用户终端手机的平台类型,如:android, ios 多个请使用逗号分隔。
string verification_code = GetMD5Str(sendno.ToString(), receiver_type.ToString(), receiver_value, masterSecret);//验证串,用于校验发送的合法性。MD5
/*
string postData = "sendno=" + sendno;
postData += ("&app_key=" + app_key);
postData += ("&masterSecret=" + masterSecret);
postData += ("&receiver_type=" + receiver_type);
postData += ("&receiver_value=" + receiver_value);
postData += ("&msg_type=" + msg_type);
postData += ("&msg_content=" + msg_content);
postData += ("&platform=" + platform);
postData += ("&verification_code=" + verification_code);
*/
string postData = "sendno=" + sendno;
postData += ("&app_key=" + app_key);
//postData += ("&masterSecret=" + masterSecret);
postData += ("&receiver_type=" + receiver_type);
postData += ("&receiver_value=" + receiver_value);
postData += ("&verification_code=" + verification_code);
postData += ("&msg_type=" + msg_type);
postData += ("&msg_content=" + msg_content);
postData += ("&platform=" + platform);
//byte[] data = encoding.GetBytes(postData);
byte[] data = Encoding.UTF8.GetBytes(postData);
string resCode = GetPostRequest(data);//调用极光的接口获取返回值
HttpContext.Current.Response.Write(resCode);
JpushMsg msg = Newtonsoft.Json.JsonConvert.DeserializeObject
/*
失败json:{"sendno":"344076014","msg_id":2554996103,"errcode":1011,"errmsg":"cannot find user by this audience"}
成功json:{"sendno":"480383770","msg_id":"847350150","errcode":0,"errmsg":"Succeed"}
*
发送自定义消息时:{"sendno":"1344179834","errcode":1003,"errmsg":"msg_content should be JSON format"}
{"sendno":"151526149","msg_id":3635937751,"errcode":1003,"errmsg":"message should be in msg_content as a JSON key"}
参考地址:https://www.doczj.com/doc/d316483029.html,/display/dev/Push-API-v3
*
*/
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
}
}
///
/// MD5加密验证字符串,用于调用接口的时候,极光将做验证使用
///
///参数数组
///
public string GetMD5Str(params string[] paras)
string str = "";
for (int i = 0; i < paras.Length; i++)
{
str += paras[i];
}
byte[] buffer = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(str));
string md5Str = string.Empty;
for (int i = 0; i < buffer.Length; i++)
{
md5Str = md5Str + buffer[i].ToString("X2");
}
return md5Str;
}
///
/// Post方式请求获取返回值
///
///
///
public string GetPostRequest(byte[] data)
{
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("https://www.doczj.com/doc/d316483029.html,:8800/v2/push");
myRequest.Method = "POST";//极光http请求方式为post
myRequest.ContentType = "application/x-www-form-urlencoded";//按照极光的要求
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close();
// Get response
var response = (HttpWebResponse)myRequest.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8")))
{
string result = reader.ReadToEnd();
reader.Close();
response.Close();
return result;
}
}
类JpushMsg代码:
using System;
using System.Collections.Generic; using System.Linq;
using System.Web;
///
///接收返回值信息
///
public class JpushMsg
{
private string sendno;//编号
public string Sendno
{
get { return sendno; }
set { sendno = value; }
}
private string msg_id;//信息编号
public string Msg_id
{
get { return msg_id; }
set { msg_id = value; } }
private string errcode;//返回码
public string Errcode
{
get { return errcode; }
set { errcode = value; } }
private string errmsg;//错误信息
public string Errmsg
{
get { return errmsg; }
set { errmsg = value; }
}
}
新加web页面Default.aspx
添加控件V3-推送:
Default.aspx.cs按钮事件代码:
protected void btnPushV3_Click(object sender, EventArgs e)
{
JPushV3 util = new JPushV3();
/*registration_id
{"platform":"all","audience":"all","notification":{"alert":"Test from C# v3 sdk - alert-你好"},"options":{"apns_production":false}}
*/
//string reqParams = "{\"platform\":\"all\",\"audience\":\"all\",\"notification\":{\"alert\":\"Test from C# v3 sdk - alert-你好\"},\"options\":{\"apns_production\":false}}";
//string reqParams =
"{\"platform\":[\"android\",\"ios\"],\"audience\":{\"tag\":[\"tag1\",\"tag2\"]},\"message\":{\"msg_content\":\"自定义消息测试:Test from C# v3 sdk -
msgContent\",\"extras\":{\"from\":\"JPush\"}},\"options\":{\"apns_production\":false}}";
//string reqParams =
"{\"platform\":[\"android\",\"ios\"],\"audience\":\"all\",\"message\":{\"msg_content\":\"自定义消息测试:Test from C# v3 sdk - msgContent\",\"title\":\"广通测试标题
\",\"extras\":{\"from\":\"JPush\"}},\"options\":{\"apns_production\":false}}";
string reqParams =
"{\"platform\":[\"android\",\"ios\"],\"audience\":{\"alias\":[\"1899\"]},\"message\":{\"msg_content\":\"自定义消息测试:您有新的公文需要办理\",\"title\":\"广通测试标题注册
ID\",\"extras\":{\"from\":\"JPush\"}},\"options\":{\"sendno\":12345678}}";
//reqParams =
"{\"platform\":[\"android\",\"ios\"],\"audience\":{\"alias\":[\"1001\"]},\"message\":{\"msg_content\":\"自定义消息测试:您有新的公文需要办理\",\"title\":\"广通测试标题注册
ID\",\"extras\":{\"from\":\"JPush\"}},\"options\":{\"apns_production\":false}}";
string jsonString = util.SendPostRequest(reqParams);
Response.Write(jsonString + "
");
string errorMessage = "";
string errorCode = "";
string sendno = "";
string msg_id = "";
bool processResult = util.IsSuccess(jsonString, out errorMessage, out errorCode, out sendno, out msg_id);
Response.Write("errorMessage:" + errorMessage + "
");
Response.Write("errorCode:" + errorCode + "
");
Response.Write("sendno:" + sendno + "
");
Response.Write("msg_id:" + msg_id + "
");
/*//遇到json的值中含有\"请使用\\\"来转义
reqParams =
"{\"platform\":[\"android\",\"ios\"],\"audience\":{\"alias\":[\"1001\"]},\"message\":{\"msg_content\":\"自定义消息测试:您有\\\"新的公文需要办理\",\"title\":\"广通测试标题注册
ID\",\"extras\":{\"from\":\"JPush\"}},\"options\":{\"apns_production\":false}}";
result = util.SendPostRequest(reqParams);
Response.Write(result + "
");*/
//string result = util.GetReceivedResult("1799597405");
//Response.Write(result + "
");
/*
* 正确时返回结果{"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}} 键区分大小写
*/
}
运行效果: