I have an application that is calling the API to send push notifications. I am getting what appears to be random 500 Internal Server Errors trying to send an Android Push. I will try to send a Push for a given JSON payload. Sometimes that payload will work and sometimes the same payload does not work and throws the error. I can't figure out why it is failing. I have actually put a try catch around the call the UA API and then if it fails, I will wait a bit and try again in a nested try catch and that call will fail. Using the same payload. I don't know how to see why it failed or figure out why it fails sometimes and not other. It makes me leery of moving this up to QA to test with random failures. Any ideas?
Random Internal Server Error when sending an Android Push Notifications
Didn't find what you were looking for?
New post-
Michael Halka
Hi George,
Do you have an example of the API requests you're making?
Additionally, are you making the requests through cURL, or using one of our server libraries to make the requests?
Comment actions -
Michael,
I don't have a public API I send you a link to, but I have included the code that I am using to call the UA api. Maybe that will help.
WebResponse response;
var encoding = new UTF8Encoding();
var request = (HttpWebRequest)WebRequest.Create(apiUrl);
request.Method = "POST";
request.Credentials = new NetworkCredential(applicationKey, applicationMasterSecret);
request.ContentType = "application/json";
request.ContentLength = encoding.GetByteCount(jsonPayload);
request.Accept = "application/vnd.urbanairship+json; version=3;";using (var stream = request.GetRequestStream())
{
stream.Write(encoding.GetBytes(jsonPayload), 0, encoding.GetByteCount(jsonPayload));
stream.Close();
response = request.GetResponse();
response.Close();
}Comment actions -
Michael Halka
George,
I've had problems with that in the past, mostly due to .NET being silly.
Here's what has worked for me in the past:public void iosPostRequest()
{
try
{
Uri uri = new Uri("https://go.urbanairship.com/api/push/");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/json";
request.Accept = "application/vnd.urbanairship+json; version=3;";
byte[] encodedPassword = System.Text.Encoding.UTF8.GetBytes("Insert App Key here" + ":" + "Insert Master Secret here");
string encodedAuto = System.Convert.ToBase64String(encodedPassword);
request.Headers["Authorization"] = "Basic " + encodedAuto;
alert = "Hello Urban Airship!";
postData = "{\"audience\": \"all\",\"notification\": {\"ios\":{\"alert\":\"Broadcast: " + alert + "\",\"badge\": \"auto\",\"sound\": \"default\"}},\"device_types\": [\"ios\"]}";
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
}
catch (WebException e)
{
Logger.Debug("Result" + e.Message);
Logger.Debug("Result" + e.Status);
}
}
private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
Stream postStream = request.EndGetRequestStream(asynchronousResult);
Logger.Debug("PostData" + postData);
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
postStream.Write(byteArray, 0,byteArray.Length);
postStream.Close();
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
private static void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
if (response.StatusCode == System.Net.HttpStatusCode.OK || response.StatusCode == System.Net.HttpStatusCode.Accepted)
{
Logger.Debug("Okay");
}
else
{
Logger.Debug("Not Okay:" + response.StatusCode);
}
Stream Answer = response.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
Console.WriteLine(_Answer.ReadToEnd());
Answer.Close();
_Answer.Close();
response.Close();
}
catch (WebException e)
{
Console.WriteLine("\nRespCallback Exception raised!");
Console.WriteLine("\nMessage:{0}", ((HttpWebResponse)e.Response).StatusCode);
Console.WriteLine("\nStatus:{0}", ((HttpWebResponse)e.Response).StatusDescription);
}
}
It's similar to what you have, with the added request stream callback and not defining the content size. Give it a shot and let me know if that helps any!Comment actions -
Michael,
Thanks for the follow. Where do you actually send the alert and postData? I may be missing it, but I don't see where it is actually sent.
Edit*... I just found it. Nevermind
alert = "Hello Urban Airship!";
postData = "{\"audience\": \"all\",\"notification\": {\"ios\":{\"alert\":\"Broadcast: " + alert + "\",\"badge\": \"auto\",\"sound\": \"default\"}},\"device_types\": [\"ios\"]}";
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);Comment actions
Please sign in to leave a comment.
Comments
4 comments