public string SendPushNotificationForAndroidWithImage(PushNotificationParameters parameters)
        {
            string response;

            try
            {
                WebRequest request = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
                request.Timeout     = parameters.RequestTimeOut;
                request.Method      = "Post";
                request.ContentType = "application/json";
                var data = new
                {
                    to   = parameters.DeviceId,
                    data = new
                    {
                        title     = parameters.Title,
                        sound     = parameters.SoundEnable,
                        messageAR = parameters.NotificationAR,
                        messageEN = parameters.NotificationEN,
                        image     = parameters.ImageURL
                    }
                };
                var json = SerializeAndroidWithImage(parameters);
                //tracingService.Trace("SendPushNotificationForAndroidWithImage : json" + json);

                Byte[] byteArray = Encoding.UTF8.GetBytes(json);
                request.Headers.Add(string.Format("Authorization: key={0}", parameters.ServerKey));
                request.Headers.Add(string.Format("Sender: id={0}", parameters.SenderId));
                request.ContentLength = byteArray.Length;
                using (Stream dataStream = request.GetRequestStream())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);


                    using (WebResponse tResponse = request.GetResponse())
                    {
                        using (Stream dataStreamResponse = tResponse.GetResponseStream())
                        {
                            using (StreamReader tReader = new StreamReader(dataStreamResponse))
                            {
                                String sResponseFromServer = tReader.ReadToEnd();
                                response = sResponseFromServer;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                response = ex.Message;
            }
            return(response);
        }
        public string SerializeAndroidWithImage(PushNotificationParameters parameters)
        {
            string result = string.Empty;

            result = "{\"to\":\"" + parameters.DeviceId
                     + "\",\"data\":{\"title\":\"" + parameters.Title
                     + "\",\"body\":\"" + parameters.Message
                     + "\",\"sound\":\"" + parameters.SoundEnable
                     + "\",\"messageAR\":\"" + parameters.NotificationAR
                     + "\",\"messageEN\":\"" + parameters.NotificationEN
                     + "\",\"image\":\"" + parameters.ImageURL + "\"}}";

            return(result);
        }
        public string SendPushNotificationForAndroidSWithoutImage()
        {
            PushNotificationParameters parameters = new PushNotificationParameters();


            parameters.DeviceId  = "c8e3X9H3SZ6ZkVfC03ydvm:APA91bG7Dwh0D0V5emRhGVWpcrWU71uw9m2Z3I7uzHSM5dDvgzNckai1F42PIfhv_0Lh6gpcmNU0OsEfoMqdfWHceleY5yXqRPlDPhY5IcUhuLRKYzyuiIHIos3ZyXVJgAN3JjmgWJmi";
            parameters.ServerKey = "AAAAW6UTX4M:APA91bFTPPT7qvSC5VyR854TRyTtzXVuSnG8V8zTu6t3mJnOhhApExR2Qphfgnue0dka3eDWh3RPPkHu6elE9GCUox-ffclJbGGS1EoedUtuetyP4Y0vRuMjlcVSOWo2K5vTrHDsrwZU";
            parameters.SenderId  = "393611534211";
            //parameters.ApplicationId = "1:393611534211:android:b1773d098572b7b910e0b6";
            parameters.RequestTimeOut = 20000;
            parameters.Title          = "TestTitle";
            parameters.SoundEnable    = "Enabled";
            parameters.NotificationEN = "TestEn";
            parameters.NotificationAR = "TestAR";
            parameters.Message        = "testMessage";
            string response;

            try
            {
                WebRequest request = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
                request.Timeout = parameters.RequestTimeOut;
                request.Method  = "Post";


                request.ContentType = "application/json";
                var data = new
                {
                    //ApplicationId = parameters.ApplicationId,
                    to                = parameters.DeviceId,
                    priority          = "high",
                    content_available = true,

                    notification = new
                    {
                        title     = parameters.Title,
                        sound     = parameters.SoundEnable,
                        messageAR = parameters.NotificationAR,
                        messageEN = parameters.NotificationEN
                    }
                };
                var json = SerializeAndroidWithoutImage(parameters);
                //tracingService.Trace("SendPushNotificationForAndroidWithoutImage: json" + json);
                Byte[] byteArray = Encoding.UTF8.GetBytes(json);

                request.Headers.Add(string.Format("Authorization:key={0}", parameters.ServerKey));
                request.Headers.Add(string.Format("Sender: id={0}", parameters.SenderId));
                request.ContentLength = byteArray.Length;
                //	tracingService.Trace("SendPushNotificationForAndroidWithoutImage : ContentLength" + byteArray.Length);

                using (Stream dataStream = request.GetRequestStream())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);
                    //	tracingService.Trace("SendPushNotificationForAndroidWithoutImage:dataStream " + dataStream.Length);

                    using (WebResponse webResponse = request.GetResponse())
                    {
                        using (Stream dataStreamResponse = webResponse.GetResponseStream())
                        {
                            using (StreamReader streamReader = new StreamReader(dataStreamResponse))
                            {
                                string sResponseFromServer = streamReader.ReadToEnd();
                                response = sResponseFromServer;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                response = ex.Message;
            }
            return(response);
        }
예제 #4
0
        public string SendPushNotificationForiOSWithoutImage()
        {
            PushNotificationParameters parameters = new PushNotificationParameters();

            parameters.DeviceId       = "26b3303e7b24aea67acf871be3f603443fa07e03";
            parameters.ServerKey      = "AAAA-03M4mA:APA91bEnieHV6VhnOAHNFEPTR-M95dl0iQMag5nxhOKA_P6dyzRZszIJBMzBp98Q1FbyYG0VTGTsFx0eAIkbkYWvTTUmhVgzskGH0i3U-TVX_BgNE0QjBudL7HnXgu6RfIxNYolBgO7M";
            parameters.SenderId       = "1079342064224";
            parameters.RequestTimeOut = 20000;
            parameters.Title          = "TestTitle";
            parameters.SoundEnable    = "default";
            parameters.NotificationEN = "TestEn";
            parameters.NotificationAR = "TestAR";
            parameters.Message        = "testMessage";
            string response;

            try
            {
                WebRequest webRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
                webRequest.Timeout = parameters.RequestTimeOut;
                webRequest.Method  = "POST";

                webRequest.ContentType = "application/json";


                var data = new
                {
                    to           = parameters.DeviceId,
                    notification = new
                    {
                        title     = parameters.Title,
                        sound     = parameters.SoundEnable,
                        messageAR = parameters.NotificationAR,
                        messageEN = parameters.NotificationEN
                    }
                };


                var json = SerializeiOSWithoutImage(parameters);


                Byte[] byteArray = Encoding.UTF8.GetBytes(json);
                webRequest.Headers.Add(string.Format("Authorization: key={0}", parameters.ServerKey));
                webRequest.Headers.Add(string.Format("Sender: id={0}", parameters.SenderId));
                webRequest.ContentLength = byteArray.Length;


                using (Stream dataStream = webRequest.GetRequestStream())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);


                    using (WebResponse webResponse = webRequest.GetResponse())
                    {
                        using (Stream dataStreamResponse = webResponse.GetResponseStream())
                        {
                            using (StreamReader streamReader = new StreamReader(dataStreamResponse))
                            {
                                String sResponseFromServer = streamReader.ReadToEnd();
                                response = sResponseFromServer;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                response = ex.Message;
            }

            return(response);
        }