views:

344

answers:

2

hi,

i"m want to send notification from my server side (c#) via urbanairship api

is there any example in c# how to do it?

thanks

+4  A: 

Basically...

using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestPostExample
    {
        public static void Main ()
        {
            // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create ("https://go.urbanairship.com/api/push/");
            // Set the Method property of the request to POST.
            request.Method = "POST";

            // Create POST data and convert it to a byte array. 

broken out to multiple lines so you can read it

            string postData = "{
    "device_tokens": [
        "some device token",
        "another device token"
    ],
    "aliases": [
        "user1",
        "user2"
    ],
    "tags": [
        "tag1",
        "tag2"
    ],
    "schedule_for": [
        "2010-07-27 22:48:00",
        "2010-07-28 22:48:00"
    ],
    "exclude_tokens": [
        "device token you want to skip",
        "another device token you want to skip"
    ],
    "aps": {
         "badge": 10,
         "alert": "Hello from Urban Airship!",
         "sound": "cat.caf"
    }
}";

and then

            byte[] byteArray = Encoding.UTF8.GetBytes (postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // 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 object.
            dataStream.Close ();
            // Get the response.
            WebResponse response = request.GetResponse ();
            // Display the status.
            Console.WriteLine (((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream ();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader (dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd ();
            // Display the content.
            Console.WriteLine (responseFromServer);
            // Clean up the streams.
            reader.Close ();
            dataStream.Close ();
            response.Close ();
        }
    }
}

See api docs, msdn and here for more on https

slf
Great answer, SLF!
Jann
+1  A: 

The accepted answer doesn't work, you need to change the following line:

request.ContentType = "application/x-www-form-urlencoded";

to

request.ContentType = "application/json";

Complete working code shown below:

using System;

using System.IO; using System.Net; using System.Text;

namespace UrbanAirship_Tes_1 { class Program { public static void Main() { /* // Create a request using a URL that can receive a post. WebRequest request = WebRequest.Create("https://go.urbanairship.com/api/push/"); request.Credentials = new NetworkCredential("application key", "master secret");

        // Set the Method property of the request to POST.
        request.Method = "POST";


        // Create POST data and convert it to a byte array. 
        string postData = "{\"aps\": {\"badge\": 1, \"alert\": \"Hello from Urban Airship!\"}, \"device_tokens\": [\"6334c016fc643baa340eca25bc661d15055a07b475e9a6108f3f641115dd05ac\"]}";

        //string postData = "{\"device_tokens\": [\"6334c016fc643baa340eca25bc661d15055a07b475e9a6108f3344b15dd05ac\"],\"aps\": {\"badge\": 10,\"alert\": \"Hello from Urban Airship!\",\"sound\": \"cat.caf\"}}";


        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded";
        // 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 object.
        dataStream.Close();
        // Get the response.
        WebResponse response = request.GetResponse();
        // Display the status.
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        // Display the content.
        Console.WriteLine(responseFromServer);
        // Clean up the streams.
        reader.Close();
        dataStream.Close();
        response.Close();
        */

        // Create a request using a URL that can receive a post. 
        WebRequest request = WebRequest.Create("https://go.urbanairship.com/api/push/");
        request.Credentials = new NetworkCredential("pvYMExk3QIO7p2YUs6BBkg", "rO3DsucETRadbbfxHkd6qw");
        // Set the Method property of the request to POST.
        request.Method = "POST";
        // Create POST data and convert it to a byte array.
        //WRITE JSON DATA TO VARIABLE D
        string postData = "{\"aps\": {\"badge\": 1, \"alert\": \"Hello from Urban Airship!\"}, \"device_tokens\": [\"6334c016fc643baa340eca25bc661d15055a07b475e9a6108f3f644b15dd05ac\"]}";
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/json";
        // 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 object.
        dataStream.Close();
        // Get the response.
        WebResponse response = request.GetResponse();
        // Display the status.
        //            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        // Display the content.
        Console.WriteLine(responseFromServer);
        // Clean up the streams.
        reader.Close();
        dataStream.Close();
        response.Close();
    }
}

}

Steven