我正在尝试使用 C# 通过 UrbanAirship 向我的 ipod 发送测试通知。我尝试了许多代码片段,试图从我的 Windows 服务器向 UrbanAirship 发送通知,但我总是以 401 错误告终。
这是我的最新代码:
string postData = "{\"aps\": {\"badge\": 1, \"alert\": \"Hello from Urban Airship!\"}, \"device_tokens\": [\""+ devToken +"\"]}";
var uri = new Uri("https://go.urbanairship.com/api/push/");
var encoding = new UTF8Encoding();
var request = (HttpWebRequest)WebRequest.Create(uri);
char[] charArray = Encoding.UTF8.GetChars(Encoding.UTF8.GetBytes(postData));
request.Method = "POST";
request.Credentials = new NetworkCredential(username, password);
request.ContentType = "application/json";
request.ContentLength = encoding.GetByteCount(charArray);
using (var stream = request.GetRequestStream())
{
stream.Write(encoding.GetBytes(postData), 0, encoding.GetByteCount(postData));
stream.Close();
var response = request.GetResponse();
response.Close();
}
注意:我已经验证我的用户名和密码是正确的。
===更新===
我会说我的城市飞艇配置有问题,但以下工作:
curl -X PUT -u "appKey:appSecret" -H "Content-Type: application/json" --data '{"alias": "myalias"}' https://go.urbanairship.com/api/device_tokens/myTOken/