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

Source: Internet
Author: User

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), the specific mobile phone development needs to do what we. NET platform regardless

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

2, read the values in the configuration

Private readonly string ApiKey = "";p rivate 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" &                gt; device number </param> public void pushandroid (string registrationid) {try {                Random ran = new random (); int Sendno = ran.                Next (1, 2100000000);//random generation of a number 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 according to the Registrationid.                Currently only the Android SDK r1.6.0 version supports string receiver_value = Registrationid; int msg_type = 1;//1, notification 2, custom message (only Android support) string msg_content = "{\" n_builder_id\ ": \" 00\ ", \" n_title\ ": \" "+ Title +" \ ", \" n_content\ ": \" "+ content +" \ "}";//message content string platform = "Android";//target user terminal phone platform type, such as: a                Ndroid, please use commas to separate multiple iOS. String verification_code = Getmd5str (Sendno. ToString (), receiver_tyPe. ToString (), receiver_value,mastersecret);//validation string for verifying the legality of the transmission.                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);//The interface that calls Aurora gets the return value jpushmsg msg = Newtonsoft.Json.JsonConvert.Deserialize            Object<jpushmsg> (Rescode);//define a JPUSHMSG class that contains the return value information and convert the returned JSON format string to 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 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 method to invoke Aurora's push service

 <summary>////Post method request get return value///</summary>//<param name= "Data" &GT;&LT;/PARAM&G        T <returns></returns> public string getpostrequest (byte[] data) {HttpWebRequest my            Request = (HttpWebRequest) webrequest.create ("Http://api.jpush.cn:8800/v2/push"); Myrequest.method = "POST";//Aurora HTTP request mode is post Myrequest.contenttype = "application/x-www-form-urlencoded";//According to Aurora Requires 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, define a class that receives the return value

public class Jpushmsg    {        private string sendno;//number public        string Sendno        {            get {return sendno;}            set {Sendno = value;}        }        private string msg_id;//information number 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;}        }    }

The. NET platform pushes Android 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.