十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決

1,首先需要將你的app在極光官網(wǎng)上進行注冊,獲取一個ApiKey,一個APIMasterSecret(密碼),將這兩個值保存在配置文件(app/web.config)中,具體手機開發(fā)端需要做什么操作我們.net平臺不管

2,讀取配置中的值
private readonly string ApiKey = "";
private readonly string APIMasterSecret = "";
ApiKey= ConfigurationManager.AppSettings["ApiKey"].ToString();//Android ApiKeyAPIMasterSecret = ConfigurationManager.AppSettings["APIMasterSecret"].ToString();//Android密碼3,開始推送方法
/// /// Android極光推送
/// /// 設(shè)備號 public void PushAndroid(string RegistrationID)
{
try
{
Random ran= new Random();
int sendno = ran.Next(1, 2100000000);//隨機生成的一個編號 string app_key = ApiKey;
string masterSecret = APIMasterSecret;
int receiver_type = 5;//接收者類型。2、指定的 tag。3、指定的 alias。4、廣播:對 app_key 下的所有用戶推送消息。5、根據(jù) RegistrationID 進行推送。當前只是 Android SDK r1.6.0 版本支持 string receiver_value = RegistrationID;
int msg_type = 1;//1、通知2、自定義消息(只有 Android 支持) string msg_content = "{"n_builder_id":"00","n_title":"" + Title + "","n_content":"" + Content + ""}";//消息內(nèi)容 string platform = "android";//目標用戶終端手機的平臺類型,如: android, ios 多個請使用逗號分隔。 string verification_code = GetMD5Str(sendno.ToString(), receiver_type.ToString(), receiver_value,masterSecret);//驗證串,用于校驗發(fā)送的合法性。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);
//byte[] data = encoding.GetBytes(postData); byte[] data = Encoding.UTF8.GetBytes(postData);
string resCode = GetPostRequest(data);//調(diào)用極光的接口獲取返回值
JpushMsg msg = Newtonsoft.Json.JsonConvert.DeserializeObject(resCode);//定義一個JpushMsg類,包含返回值信息,將返回的json格式字符串轉(zhuǎn)成JpushMsg對象
}
catch (Exception ex)
{
}
} 4,MD5加密驗證字符串,用于調(diào)用接口的時候,極光將做驗證使用
/// /// MD5字符串
/// /// 參數(shù)數(shù)組 /// MD5字符串 public string GetMD5Str(params string [] paras)
{
string str = "";
for(int i=0;i5,http Post方式調(diào)用極光的推送服務(wù)
/// /// Post方式請求獲取返回值
/// /// /// public string GetPostRequest(byte[] data)
{
HttpWebRequest myRequest= (HttpWebRequest)WebRequest.Create("http://api.jpush.cn: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;
}
}6,定義一個類,接收返回值
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; }
}
}好了,OK了,有什么不對的地方希望大家指出,謝謝!