Background
Within my C# middle tier, I have a scheduled task that runs certain business logic every minute and then sends push notifications to iOS and Android devices accordingly. I use Urban Airship to facilitate the push notices which means that, in my code, I am just calling their API and then they handle the actual pushing to various devices. Here is what that code looks like:
public static void SendNotice(UaMessageModel messages)
{
var audience = new NameValueCollection
{
{"alias", string.Join(", ", messages.UserIds)}
};
var notification = new NameValueCollection
{
{"alert", messages.Message}
};
var pushNotice = new NameValueCollection
{
{"audience", new JavaScriptSerializer().Serialize(audience)},
{"notification", new JavaScriptSerializer().Serialize(notification)}
};
//ToDo: remove "validate" from URL when done testing.
Utilities.PostWebRequest("https://go.urbanairship.com/api/push/validate/", "Basic XXXXXXXXX", pushNotice, "application/vnd.urbanairship+json; version=3;");
}
public class UaMessageModel
{
public List<int> UserIds { get; set; }
public string Message { get; set; }
}
Problem
When I put a break point at Utilities.PostWebRequest
and hover over pushNotice
, I only see the keys of the pushNotice
NameValueCollection, but no values. Am I constructing this NameValueCollection incorrectly?
More Info
Whether pushNotice
is constructed correctly or not, I still don't get any response from my WebClient
post. This method works when I use just a simple, flat post object. Here is that code:
public static dynamic PostWebRequest(string url, string token, NameValueCollection obj, string accept)
{
using (var wc = new WebClient())
{
wc.Headers.Add("Authorization", token);
wc.Headers.Add("Accept", accept);
return GetObjectFromJson(Encoding.UTF8.GetString(wc.UploadValues(url, "POST", obj)));
}
}
Thanks in advance.