The. NET platform pushes Android,ios messages with third-party push services (Aurora push)

Source: Internet
Author: User
Tags http post md5 encryption

Recently done. NET project (Windows Service) need to send push message to Android phone, it is a bit difficult, did not stop to search the document, and finally saw an open source project Pushsharp, Can push the ios,android,windows phone and other devices in the. NET platform message, exultation, and then did the iOS, success, but do the Android when encountered problems, has been pushed unsuccessful, the program was executed, but the push has not come out, and then struggled to search the internet , did not find, finally abandoned the use of this push Android, another way out, then found a C2DM cloud push function, but the problem arises, (1) C2DM built into Android 2.2 system, can not be compatible with the old 1.6 to 2.1 system; (2) C2DM need to rely on Google's official C2DM server, due to the domestic network environment, this service is often not available, if you want to use it well, our app server must also be abroad, this is probably not every developer can achieve; (3) Unlike in the iphone, they integrate hardware systems together. So for our developers, if we want to use C2DM push in our application, because of the different hardware vendors platform, such as Motorola, Huawei, ZTE do a mobile phone, they may be Google's services to remove, especially as in the domestic many such, Get rid of this native service from Google. Buying something like a cottage or a Huawei-made machine, Google's services may not be available. And things like those out there might be built in. No way, finally to the third-party push service platform, Aurora Push, below will explain how to use the Aurora push.

1, first need to register your app on the Aurora official website, get a apikey, a apimastersecret (password), save these two values in the configuration file (app/ Web. config), what needs to be done by the specific mobile phone developer our. NET platform regardless of

< appSettings >    <  key= "ApiKey"  value= "**********"/>    < Key = "Apimastersecret"  value= "*******"/>    </appSettings>

2 . Read the values in the configuration

 private  readonly  string  ApiKey =  ;  private  readonly  string  Apimastersecret =  ; ApiKey  = Configurationmanager.appsettings[ " apikey   "]. ToString (); // android ApiKey  Apimastersecret = Configurationmanager.appsettings[ " apimastersecret   "]. ToString (); // android password  

3. start the Push method

/// <summary>        ///Android Aurora Push/// </summary>        /// <param name= "Registrationid" >Device number</param>         Public voidPushandroid (stringRegistrationid) {            Try{Random ran=NewRandom (); intSendno = ran. Next (1,2100000000);//a randomly generated number                stringApp_key =ApiKey; stringMastersecret =Apimastersecret; intReceiver_type =5;//The recipient type. 2, the specified tag. 3, the specified alias. 4. Broadcast: Push messages to all users under App_key. 5, push according to the Registrationid. Currently just Android SDK r1.6.0 version Support                stringReceiver_value =Registrationid; intMsg_type =1;//1, notification 2, custom messages (only Android support)                stringMsg_content ="{\ "n_builder_id\": \ "00\", \ "n_title\": \ ""+ Title +"\ ", \" n_content\ ": \""+ Content +"\"}";//Message Content                stringPlatform ="Android";//Target user terminal phone platform type, such as: Android, iOS multiple please use comma separated.                 stringVerification_code = Getmd5str (Sendno. ToString (), Receiver_type. ToString (), Receiver_value,mastersecret);//validates the string to verify the legality of the Send. MD5                stringPostData ="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); stringRescode = getpostrequest (data);//call the Aurora's interface to get the return valueJpushmsg msg = newtonsoft.json.jsonconvert.deserializeobject<jpushmsg> (Rescode);//defines a jpushmsg class that contains return value information and converts the returned JSON format string into a Jpushmsg object            }            Catch(Exception ex) {}}
4.MD5 encryption validation string, used to invoke the interface when the Aurora will do the verification using
/// <summary>        ///MD5 String/// </summary>        /// <param Name= "Paras" >parameter Array</param>        /// <returns>MD5 String</returns>         Public stringGETMD5STR (params string[] paras) {            stringstr ="";  for(intI=0; I<paras. length;i++) {str+=Paras[i]; }            byte[] buffer =MD5. Create ().            ComputeHash (Encoding.UTF8.GetBytes (str)); stringMd5str =string.            Empty;  for(inti =0; I < buffer. Length; i++) {Md5str= Md5str + buffer[i]. ToString ("X2"); }            returnMd5str; }
5.HTTP POST method to invoke Aurora's push service
/// <summary>        ///Post method request get return value/// </summary>        /// <param name= "Data" ></param>        /// <returns></returns>         Public stringGetpostrequest (byte[] data) {HttpWebRequest Myrequest= (HttpWebRequest) webrequest.create ("Http://api.jpush.cn:8800/v2/push"); Myrequest.method="POST";//Aurora HTTP Request mode is postMyrequest.contenttype ="application/x-www-form-urlencoded";//According to Aurora's requirementsMyrequest.contentlength =data.            Length; Stream Newstream=Myrequest.getrequeststream (); //Send the data.Newstream.write (data,0, data.            Length);             Newstream.close (); //Get Response            varResponse =(HttpWebResponse) myrequest.getresponse (); using(varReader =NewStreamReader (response. GetResponseStream (), Encoding.GetEncoding ("UTF-8")))            {                stringresult =Reader.                ReadToEnd (); Reader.                Close (); Response.                Close (); returnresult; }        }
6.Defines a class that receives a return value

 Public classjpushmsg {Private stringSendno;//numbering          Public stringSendno {Get{returnSendno;} Set{Sendno =value;} }        Private stringmsg_id;//Information Number          Public stringmsg_id {Get{returnmsg_id;} Set{msg_id =value;} }        Private stringErrcode;//Return Code          Public stringErrcode {Get{returnErrcode;} Set{Errcode =value;} }        Private stringErrMsg;//error Message          Public stringerrmsg {Get{returnerrmsg;} Set{errmsg =value;} }    }

The. NET platform pushes Android,ios messages with third-party push services (Aurora push)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.