Send push messages to Android phone from. NET project (Windows Service)

Source: Internet
Author: User
Tags http post http request json md5 tostring


The recently made. NET project (Windows Service) needs to send a push message to the Android phone, it's a little hard to keep searching the document, and finally see an open source project Pushsharp, Can be in the. NET platform to push ios,android,windows phone and other equipment news, exultation, and then do the iOS, successful, but do Android encountered problems, has been pushed unsuccessful, the program executed, but the push has not come out, and then laborious in the online search , did not find, finally gave up using this push Android, find another way, and then found a C2DM cloud push function, but the problem again, (1) C2DM on the Android 2.2 system, not compatible with the old 1.6 to 2.1 systems; (2) C2DM need to rely on Google's official C2DM server, due to the domestic network environment, this service is often unavailable, if you want to use, our app server must also be abroad, this may not be possible for every developer to achieve; (3) Unlike in the iphone, they integrate hardware systems together. So for our developers, if we want to use C2DM push in our applications, because for different hardware vendors like Motorola, they may be able to remove this service from Google, especially at home. Remove Google's native service. Bought some like what the cottage machine or such domestic machine, may Google's services are not. And like those out there might be built in. No way, finally transferred to the third party push service platform, Aurora Push, the following will introduce how to use the Aurora push.



1, first you need to register your app on the Aurora official web site, get a apikey, a apimastersecret (password), save the two values in the configuration file (App/web.config), the specific mobile phone development side need to do what we. NET platform regardless



<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 Apikey



Apimastersecret = configurationmanager.appsettings["Apimastersecret"]. ToString ();//android password



3, Start push method


<summary>///android Aurora push///</summary>///<param name= "Registrationid" > Set
                Standby </param> public void pushandroid (string registrationid) {try {
                Random ran = new Random (); int Sendno = ran.
                Next (1, 2100000000);//randomly generated a numbered string app_key = Apikey;
                string mastersecret = Apimastersecret; int receiver_type = 5;//receiver type. 2, the designated tag. 3, the specified alias. 4. Broadcast: Push message to all users under App_key. 5, according to Registrationid push.
    
                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 supported) string msg_content = "{\ n_builder_id\": \ "00\", \ "n_title\": \ "" + Title + "\", \ "n_content\": \ "" + content + "\"};//message content//view this column more wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/ Os/extra/string platform = "Android";//target user terminal phone platform type, such as: Android, iOS multiple please use commaSeparated. String verification_code = Getmd5str (Sendno. ToString (), Receiver_type. ToString (), Receiver_value,mastersecret),//validation string, for verifying the legality 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 get the return value jpushmsg msg = Newtonsoft.Json.JsonConvert.Deserializ EOBject<jpushmsg> (Rescode),//defines a jpushmsg class, contains return value information, converts the returned JSON format string to Jpushmsg object} catch (Exceptio n ex) {}}


4,MD5 encrypted authentication string used to invoke the interface when the Aurora will do the validation 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 way to call Aurora's push service


<summary>///Post method request fetch return value///</summary>///<param name= "Data" ></para
            m>///<returns></returns> public string getpostrequest (byte[] data) {
    
            HttpWebRequest myrequest = (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";/by Aurora The request 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; }
        }




Related Article

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.