private static async Task <HttpWebResponse> GetNotificationTelemtry(string id, string hubname, string connectionString)
        {
            string hubResource = "messages/" + id + "?";
            string apiVersion  = "api-version=2015-04";
            ConnectionStringUtility connectionSasUtil = new ConnectionStringUtility(connectionString);

            //=== Generate SaS Security Token for Authentication header ===
            // Determine the targetUri that we will sign
            string uri      = connectionSasUtil.Endpoint + hubname + "/" + hubResource + apiVersion;
            string SasToken = connectionSasUtil.getSaSToken(uri, 60);

            return(await ExecuteREST("GET", uri, SasToken));
        }
Beispiel #2
0
        private static async Task <string> GetPlatformNotificationServiceFeedbackContainer(string hubName, string connectionString)
        {
            HttpWebResponse         response          = null;
            ConnectionStringUtility connectionSasUtil = new ConnectionStringUtility(connectionString);

            string hubResource = "feedbackcontainer?";
            string apiVersion  = "api-version=2015-04";

            //=== Generate SaS Security Token for Authentication header ===
            // Determine the targetUri that we will sign
            string uri = connectionSasUtil.Endpoint + hubName + "/" + hubResource + apiVersion;

            // 10 min expiration
            string SasToken = connectionSasUtil.getSaSToken(uri, 10);

            response = await ExecuteREST("GET", uri, SasToken);

            if ((int)response.StatusCode != 200)
            {
                Console.WriteLine(string.Format("Failed to get PNS feedback contaioner URI - Http Status {0} : {1}",
                                                (int)response.StatusCode, response.StatusCode));

                // Get the stream associated with the response.
                Stream errorStream = response.GetResponseStream();

                // Pipes the stream to a higher level stream reader with the required encoding format.
                StreamReader errorReader = new StreamReader(errorStream, Encoding.UTF8);
                Console.WriteLine("\n" + errorReader.ReadToEnd());

                return(null);
            }

            // Get the stream associated with the response.
            Stream receiveStream = response.GetResponseStream();

            // Pipes the stream to a higher level stream reader with the required encoding format.
            StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);

            Console.WriteLine("");
            string containerUri = readStream.ReadToEnd();

            readStream.Close();
            receiveStream.Close();
            return(containerUri);
        }
        private static async Task <string> SendNativeNotificationREST(string hubname, string connectionString, string message, string nativeType)
        {
            ConnectionStringUtility connectionSaSUtil = new ConnectionStringUtility(connectionString);
            string location = null;

            string hubResource    = "messages/?";
            string apiVersion     = "api-version=2015-04";
            string notificationId = "Failed to get Notification Id";

            //=== Generate SaS Security Token for Authentication header ===
            // Determine the targetUri that we will sign
            string uri = connectionSaSUtil.Endpoint + hubname + "/" + hubResource + apiVersion;

            // 10 min expiration
            string SasToken = connectionSaSUtil.getSaSToken(uri, 10);

            WebHeaderCollection headers = new WebHeaderCollection();
            string          body;
            HttpWebResponse response = null;

            switch (nativeType.ToLower())
            {
            case "apns":
                headers.Add("ServiceBusNotification-Format", "apple");
                body     = "{\"aps\":{\"alert\":\"" + message + "\"}}";
                response = await ExecuteREST("POST", uri, SasToken, headers, body);

                break;

            case "gcm":
                headers.Add("ServiceBusNotification-Format", "gcm");
                body     = "{\"data\":{\"message\":\"" + message + "\"}}";
                response = await ExecuteREST("POST", uri, SasToken, headers, body);

                break;

            case "wns":
                headers.Add("X-WNS-Type", "wns/toast");
                headers.Add("ServiceBusNotification-Format", "windows");
                body = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
                       "<toast>" +
                       "<visual>" +
                       "<binding template=\"ToastText01\">" +
                       "<text id=\"1\">" +
                       message +
                       "</text>" +
                       "</binding>" +
                       "</visual>" +
                       "</toast>";
                response = await ExecuteREST("POST", uri, SasToken, headers, body, "application/xml");

                break;
            }

            char[] seps1 = { '?' };
            char[] seps2 = { '/' };

            if ((int)response.StatusCode != 201)
            {
                return(string.Format("Failed to get notification message id - Http Status {0} : {1}", (int)response.StatusCode, response.StatusCode.ToString()));
            }

            location = response.Headers.Get("Location");
            string[] locationUrl   = location.Split(seps1);
            string[] locationParts = locationUrl[0].Split(seps2);

            notificationId = locationParts[locationParts.Length - 1];

            return(notificationId);
        }