Beispiel #1
0
        private static async Task <HttpWebResponse> GetNotificationTelemetry(string id, string hubName, string connectionString)
        {
            var hubResource       = "messages/" + id + "?";
            var apiVersion        = "api-version=2015-04";
            var connectionSasUtil = new ConnectionStringUtility(connectionString);

            //=== Generate SaS Security Token for Authentication header ===
            // Determine the targetUri that we will sign
            var uri      = connectionSasUtil.Endpoint + hubName + "/" + hubResource + apiVersion;
            var 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;
            var             connectionSasUtil = new ConnectionStringUtility(connectionString);

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

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

            // 10 min expiration
            var sasToken = connectionSasUtil.GetSaSToken(uri, 10);

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

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

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

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

                return(null);
            }

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

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

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

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

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

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

            // 10 min expiration
            var 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 (response != null && response.StatusCode != HttpStatusCode.Created)
            {
                return(string.Format("Failed to get notification message id - Http Status {0} : {1}", (int)response.StatusCode, response.StatusCode.ToString()));
            }

            if ((location = response.Headers.Get("Location")) != null)
            {
                var locationUrl   = location.Split(seps1);
                var locationParts = locationUrl[0].Split(seps2);

                notificationId = locationParts[locationParts.Length - 1];

                return(notificationId);
            }

            return(null);
        }
        private static async Task <string> SendNativeNotificationREST(string hubName, string connectionString, string message, string title, string nativeType, string tag)
        {
            var connectionSaSUtil = new ConnectionStringUtility(connectionString);

            var hubResource = "messages/?";
            var apiVersion  = "api-version=2015-04";

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

            // 10 min expiration
            var sasToken = connectionSaSUtil.GetSaSToken(uri, 10);

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

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

                break;

            case "android":
                headers.Add("ServiceBusNotification-Format", "gcm");
                headers.Add("ServiceBusNotification-Tags", tag);
                body     = "{\"data\":{\"title\":\"" + title + "\", \"body\":\"" + message + "\", \"priority\":\"high\"}}";
                response = await ExecuteREST("POST", uri, sasToken, headers, body);

                break;
            }

            if (response != null && response.StatusCode == HttpStatusCode.Created)
            {
                return("Notification successfully sent");
            }
            else if (response != null && response.StatusCode == HttpStatusCode.BadRequest)
            {
                return("The request is malformed (for example, not valid routing headers, not valid content-type, message exceeds size, bad message format)");
            }
            else if (response != null && response.StatusCode == HttpStatusCode.Unauthorized)
            {
                return("Authorization failure. The access key was incorrect.");
            }
            else if (response != null && response.StatusCode == HttpStatusCode.Forbidden)
            {
                return("Quota exceeded or message too large; message was rejected.");
            }
            else if (response != null && response.StatusCode == HttpStatusCode.NotFound)
            {
                return("No message branch at the URI.");
            }
            else if (response != null && response.StatusCode == HttpStatusCode.RequestEntityTooLarge)
            {
                return("Requested entity too large. The message size cannot be over 64 Kb.");
            }

            return(null);
        }