The. net platform uses third-party push services to push Android, IOS messages (Aurora push), and androidios
Recently done. net project (Windows Service) needs to send push messages to the Android mobile phone, it is a bit difficult, without having to do so, it will not stop searching documents, finally saw an open-source project PushSharp, can be in. net platform pushes messages to devices such as IOS, Android, and Windows Phone, and then performs IOS first. This is a success. However, when you do Android, the push fails, the program was executed, but the push was always available. Later, I searched online and did not find it. Finally, I gave up using this push Android and found another way. Then I found a C2DM cloud push function, however, the problem arises again. (1) C2DM is built into the Android 2.2 System and cannot be compatible with the old 1.6 to 2.1 systems; (2) c2DM depends on the C2DM Server officially provided by Google. Due to the domestic network environment, this service is often unavailable. If you want to use it well, our App Server must also be abroad, this may not be implemented by every developer; (3) Unlike in iP In hone, they integrated the hardware system. Therefore, for our developers, if we want to use the C2DM push function in our applications, because different hardware vendor platforms, such as Motorola, Huawei, and ZTE, make a mobile phone, they may remove such services as Google, especially in China, and remove native services such as Google. If you have bought some hosts such as a cottage or Huawei, Google's services may be unavailable. For example, those who go abroad may be built in. No way, finally go to the third-party PUSH Service Platform, Aurora push. The following describes how to use Aurora push.
1,First, you need to register your app on the Aurora official website to obtain an ApiKey and an APIMasterSecret (password), and save these two values in the configuration file (app/web. config. net platform
<appSettings> <add key="ApiKey" value="**********"/> <add key="APIMasterSecret" value="*******"/> </appSettings>
2,Read the value in the configuration
Private readonly string ApiKey = ""; private readonly string APIMasterSecret = ""; ApiKey = ConfigurationManager. appSettings ["ApiKey"]. toString (); // Android ApiKeyAPIMasterSecret = ConfigurationManager. appSettings ["APIMasterSecret"]. toString (); // Android Password
3,Start push Method
/// <Summary> /// Android Aurora push /// </summary> /// <param name = "RegistrationID"> device ID </param> public void PushAndroid (string RegistrationID) {try {Random ran = new Random (); int sendno = ran. next (1, 2100000000); // a randomly generated string app_key = ApiKey; string masterSecret = APIMasterSecret; int receiver_type = 5; // receiver type. 2. The specified tag. 3. The specified alias. 4. Broadcast: Push messages to all users under app_key. 5. Push by RegistrationID. Currently, only Android SDK r1.6.0 supports string receiver_value = RegistrationID; int msg_type = 1; // 1, notification 2, and custom messages (only supported by Android) string msg_content = "{\" n_builder_id \ ": \" 00 \ ", \" n_title \ ": \" "+ Title +" \ ", \" n_content \": \ "" + Content + "\"} "; // message Content string platform =" android "; // platform type of the target user's terminal mobile phone, such as android, separate multiple ios instances with commas. String verification_code = GetMD5Str (sendno. ToString (), receiver_type.ToString (), receiver_value, masterSecret); // The verification string used to verify the validity of sending. 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); // call the Aurora interface to obtain the returned value JpushMsg msg = Newtonsoft. json. jsonConvert. deserializeObject <JpushMsg> (resCode); // defines a JpushMsg class that contains the returned value and converts the returned json string to a JpushMsg object} catch (Exception ex ){}}
4,MD5 encryption verification string, used to call the interface, Aurora will be used for verification
/// <Summary> /// MD5 string /// </summary> /// <param name = "paras"> parameter array </param> /// <returns> MD5 string </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,Call Aurora PUSH Service in http Post Mode
/// <Summary> /// obtain the returned value in Post mode // </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"; // The Aurora http Request Method is post myRequest. contentType = "application/x-www-form-urlencoded"; // myRequest according to Aurora requirements. 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,Define a class and receive the returned value
Public class JpushMsg {private string sendno; // No. public string Sendno {get {return sendno;} set {sendno = value ;}} private string msg_id; // Information No. public string Msg_id {get {return msg_id;} set {msg_id = value;} private string errcode; // return code public string Errcode {get {return errcode ;} set {errcode = value ;}} private string errmsg; // error message public string Errmsg {get {return errmsg ;}set {errmsg = value ;}}}