コード例 #1
0
        /// <summary>
        /// SendPush
        /// </summary>
        /// <param name="push"></param>
        public static void SendPush(PushyPushRequest push)
        {
            // Create an HTTP request to the Pushy API
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(EwalletConstant.ApiPath + EwalletConstant.PushNotificationPath + EwalletConstant.SecretPushNotificationApiKey);

            // Send a JSON content-type header
            request.ContentType = "application/json";

            // Set request method to POST
            request.Method = "POST";

            // Convert request post body to JSON (using JSON.NET package from Nuget)
            string postData = JsonConvert.SerializeObject(push);

            // Convert post data to a byte array
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            // Set the ContentLength property of the WebRequest
            request.ContentLength = byteArray.Length;

            // Get the request stream
            Stream dataStream = request.GetRequestStream();

            // Write the data to the request stream
            dataStream.Write(byteArray, 0, byteArray.Length);

            // Close the stream
            dataStream.Close();

            // Proceed with caution
            WebResponse response;

            try
            {
                // Execute the request
                response = request.GetResponse();
            }
            catch (WebException exc)
            {
                // Get returned JSON error as string
                string errorJSON = new StreamReader(exc.Response.GetResponseStream()).ReadToEnd();

                // Parse into object
                PushyAPIError error = JsonConvert.DeserializeObject <PushyAPIError>(errorJSON);

                // Throw error
                throw new Exception(error.error);
            }

            // Open the stream using a StreamReader for easy access
            StreamReader reader = new StreamReader(response.GetResponseStream());

            // Read the response JSON for debugging
            string responseData = reader.ReadToEnd();

            // Clean up the streams
            reader.Close();
            response.Close();
            dataStream.Close();
        }
コード例 #2
0
        /// <summary>
        /// SendSamplePush
        /// </summary>
        /// <param name="deviceTokens"></param>
        /// <param name="message"></param>
        /// <param name="CarPlate"></param>
        public static void PushNotification(List <string> deviceTokens, string message)
        {
            // Prepare array of target device tokens
            // List<string> deviceTokens = new List<string>();

            // Add your device tokens here
            //deviceTokens.Add("4dac95c75b78693ca8ce42");

            // Convert to string[] array
            string[] to = deviceTokens.ToArray();

            // Optionally, send to a publish/subscribe topic instead
            // string to = '/topics/news';

            // Set payload (it can be any object)
            var payload = new Dictionary <string, string>();

            // Add message parameter to dictionary
            payload.Add("message", message);

            // iOS notification fields
            var notification = new Dictionary <string, object>();

            notification.Add("badge", 1);
            notification.Add("sound", "ping.aiff");
            notification.Add("body", "Hello World \u270c");

            // Prepare the push HTTP request
            PushyPushRequest push = new PushyPushRequest(payload, to, notification);

            try
            {
                // Send the push notification
                PushyAPI.SendPush(push);
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }