最近作的.net項目(Windows Service)須要向Android手機發送推送消息,真是有點困難,沒有搞過就不停的搜文檔,最後看到了一個開源項目PushSharp,能夠在.net平臺推送IOS,Android,Windows Phone等設備消息,大喜,而後先作了IOS的,成功了,可是作Android的時候遇到了問題,一直推送不成功,程序執行了,可是推送一直出不來,後來費勁的在網上搜,沒有找到,最後放棄使用這種推送Android,另尋出路,隨後找到了一種C2DM雲端推送功能,可是問題又出現了,(1)C2DM內置於Android的2.2系統上,沒法兼容老的1.6到2.1系統;(2)C2DM須要依賴於Google官方提供的C2DM服務器,因爲國內的網絡環境,這個服務常常不可用,若是想要很好的使用,咱們的App Server必須也在國外,這個恐怕不是每一個開發者都可以實現的;(3)不像在iPhone中,他們把硬件系統集成在一塊了。因此對於咱們開發者來講,若是要在咱們的應用程序中使用C2DM的推送功能,由於對於不一樣的這種硬件廠商平臺,好比摩托羅拉、華爲、中興作一個手機,他們可能會把Google的這種服務去掉,尤爲像在國內就不少這種,把Google這種原生的服務去掉。買了一些像什麼山寨機或者是華爲這種國產機,可能Google的服務就沒有了。而像在國外出的那些可能會內置。沒辦法了,最後轉到了第三方推送服務平臺,極光推送,下面將介紹怎麼使用極光推送。html
1,首先須要將你的app在極光官網上進行註冊,獲取一個ApiKey,一個APIMasterSecret(密碼),將這兩個值保存在配置文件(app/web.config)中,具體手機開發端須要作什麼操做咱們.net平臺無論android
<appSettings> <add key="ApiKey" value="**********"/> <add key="APIMasterSecret" value="*******"/> </appSettings>
2,讀取配置中的值ios
private readonly string ApiKey = ""; private readonly string APIMasterSecret = ""; ApiKey = ConfigurationManager.AppSettings["ApiKey"].ToString();//Android ApiKey APIMasterSecret = ConfigurationManager.AppSettings["APIMasterSecret"].ToString();//Android密碼
3,開始推送方法web
/// <summary> /// Android極光推送 /// </summary> /// <param name="RegistrationID">設備號</param> 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;//接收者類型。二、指定的 tag。三、指定的 alias。四、廣播:對 app_key 下的全部用戶推送消息。五、根據 RegistrationID 進行推送。當前只是 Android SDK r1.6.0 版本支持 string receiver_value = RegistrationID; int msg_type = 1;//一、通知二、自定義消息(只有 Android 支持) string msg_content = "{\"n_builder_id\":\"00\",\"n_title\":\"" + Title + "\",\"n_content\":\"" + Content + "\"}";//消息內容 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); //byte[] data = encoding.GetBytes(postData); byte[] data = Encoding.UTF8.GetBytes(postData); string resCode = GetPostRequest(data);//調用極光的接口獲取返回值 JpushMsg msg = Newtonsoft.Json.JsonConvert.DeserializeObject<JpushMsg>(resCode);//定義一個JpushMsg類,包含返回值信息,將返回的json格式字符串轉成JpushMsg對象 } catch (Exception ex) { } }
4,MD5加密驗證字符串,用於調用接口的時候,極光將作驗證使用json
/// <summary> /// MD5字符串 /// </summary> /// <param name="paras">參數數組</param> /// <returns>MD5字符串</returns> 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; }
5,http Post方式調用極光的推送服務api
/// <summary> /// Post方式請求獲取返回值 /// </summary> /// <param name="data"></param> /// <returns></returns> 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; } } }