Beispiel #1
0
        public string SendMessage(string RegistrationID, PushMessage Message, string AuthString)
        {

            //-- Create C2DM Web Request Object --//
            HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://android.clients.google.com/c2dm/send");

            Request.Method = "POST";
            Request.KeepAlive = false;

            //-- Create Query String --//
            NameValueCollection postFieldNameValue = new NameValueCollection();
            postFieldNameValue.Add("registration_id", RegistrationID);
            postFieldNameValue.Add("collapse_key", "1");
            postFieldNameValue.Add("delay_while_idle", "0");
            // postFieldNameValue.Add("data.message", Message);
            postFieldNameValue.Add("data.payload", Message.message);
         //   postFieldNameValue.Add("type", Message.type);
            string postData = GetPostStringFrom(postFieldNameValue);
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);


            Request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
            Request.ContentLength = byteArray.Length;

            Request.Headers.Add(HttpRequestHeader.Authorization, "GoogleLogin auth=" + AuthString);
            //Request.Headers.Add(HttpRequestHeader.Authorization, "AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA");
          
            /*if multiple project */
            

            Request.Headers.Add(HttpRequestHeader.Authorization, "AIzaSyChZCTQ0seSaeTmgkC4zltqQDhGWCYyIXQ");
            //-- Delegate Modeling to Validate Server Certificate --//
            ServicePointManager.ServerCertificateValidationCallback += delegate(
                        object
                        sender,
                        System.Security.Cryptography.X509Certificates.X509Certificate
                        pCertificate,
                        System.Security.Cryptography.X509Certificates.X509Chain pChain,
                        System.Net.Security.SslPolicyErrors pSSLPolicyErrors)
            {
                return true;
            };

            //-- Create Stream to Write Byte Array --// 
            Stream dataStream = Request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            //-- Post a Message --//
            WebResponse Response = Request.GetResponse();
            HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode;
            if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden))
            {
                return "Unauthorized - need new token";

            }
            else if (!ResponseCode.Equals(HttpStatusCode.OK))
            {
                return "Response from web service isn't OK";
                //Console.WriteLine("Response from web service not OK :");
                //Console.WriteLine(((HttpWebResponse)Response).StatusDescription);
            }

            StreamReader Reader = new StreamReader(Response.GetResponseStream());
            string responseLine = Reader.ReadLine();
            Reader.Close();

            return responseLine;
        }
Beispiel #2
0
        public string SendMessageDemo(string RegistrationID, PushMessage Message, string AuthString)
        {
            //-- Create GCM Web Request Object --//
            HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
            Request.Method = "POST";
            Request.KeepAlive = false;
            Request.ContentType = "application/json";

            PushData msg = new PushData();
            msg.registration_ids = RegistrationID;
            msg.message = "testing";
            //message.data.Add("gcm_message", "12345");
            var jsonMessage = Newtonsoft.Json.JsonConvert.SerializeObject(msg);
            byte[] byteArray = Encoding.UTF8.GetBytes(jsonMessage);
            Request.ContentLength = byteArray.Length;


            Request.Headers.Add(HttpRequestHeader.Authorization, "AIzaSyChZCTQ0seSaeTmgkC4zltqQDhGWCYyIXQ");

            //-- Delegate Modeling to Validate Server Certificate --//
            ServicePointManager.ServerCertificateValidationCallback += delegate(
                        object
                        sender,
                        System.Security.Cryptography.X509Certificates.X509Certificate
                        pCertificate,
                        System.Security.Cryptography.X509Certificates.X509Chain pChain,
                        System.Net.Security.SslPolicyErrors pSSLPolicyErrors)
            {
                return true;
            };

            //-- Create Stream to Write Byte Array --// 
            Stream dataStream = Request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            //-- Post a Message --//
            WebResponse Response = Request.GetResponse();
            HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode;
            if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden))
            {
                return "Unauthorized - need new token";

            }
            else if (!ResponseCode.Equals(HttpStatusCode.OK))
            {
                return "Response from web service isn't OK";
            }

            StreamReader Reader = new StreamReader(Response.GetResponseStream());
            string responseLine = Reader.ReadLine();
            Reader.Close();

            return responseLine;
        }
        public Response<string> SendPush(push objReq)
        {
            Response<string> response = new Response<string>();
            List<string> objResp = new List<string>();
            try
            {


                if (CheckRequestIsvalidornot(this.Request))
                {
                    if (!string.IsNullOrEmpty(objReq.deviceToken))
                    {
                        PushMessage objPush = new PushMessage();
                        objPush.message = PushMessages.newRequest;
                        // objPush.type = PushType.newRequest.ToString();

                        PushData push = new PushData();
                        push.message = "Testing";
                        push.registration_ids = objReq.deviceToken;
                        // push.data = CommonMethod.ObjectToJson(objPush);
                        push.type = Convert.ToInt16(PushType.acceptRequest).ToString();
                        if (objReq.deviceType == "1")//ios
                        {
                            SendPush objIOS = new SendPush();
                            objIOS.ConnectToAPNS(push);
                        }
                        else if (objReq.deviceType == "2")//android
                        {
                            Android objAndroid = new Android();
                            objAndroid.send(push);

                        }
                    }
                    else
                        response.Create(false, ModelState.Values.FirstOrDefault().Errors.FirstOrDefault().ErrorMessage, Messages.AppVersion, objResp);
                }
                else
                    response.Create(false, Messages.FormatMessage(Messages.InvalidReq), Messages.AppVersion, objResp);

            }
            catch (Exception ex)
            {
                object session = new JavaScriptSerializer().Serialize(objReq);
                LogManager.Error("Error occured while Processing Webservice request :{0}", ex, session, ex.Message);
                response.Create(false, Messages.FormatMessage(Messages.ErrorOccure), Messages.AppVersion, objResp);
            }
            finally
            {
            }
            return response;
        }