I have created a Push Notifications for Android using GCM.
The Service is facing many timeouts and it works intermittently.
I have a Windows service which runs at an arbitary time(30 Secs in this case.) and checks the Message Queue , and if any messages existing it sends it to the respective device.
The Pattern is :
It works for first two three times and if
Once it fails , it starts to fail continuously.
What could be the reason for failure ?
EDIT
I thought this may be occuring due to organisation’s firewall but that also can be over ruled since i have tried it on a open network.
Any inputs any thinking lines would be helpful.
THANKS in Advance
Code :
private void SendAndroidMessage(string deviceID,string Message ,DeviceDataModel ddm)
{
StringBuilder postMsgBuilder = new StringBuilder();
postMsgBuilder.Append("registration_id=").Append(deviceID);
//postMsgBuilder.Append("&").Append("delay_while_idle=").Append("false");
postMsgBuilder.Append("&").Append("data.message=").Append(Message);
byte[] messageBytes = Encoding.UTF8.GetBytes(postMsgBuilder.ToString());
var request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
// Hook a callback to verify the remote certificate
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(C2dmCertValidationCallback);
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
request.ContentLength = messageBytes.Length;
request.Headers.Add(HttpRequestHeader.Authorization, "key=" + AuthorisationKey);
bool sent = false;
int tries = 0;
HttpWebResponse response;
while (!sent && tries < this.numOfRetries)
{
try
{
using (var requestStream = request.GetRequestStream())
{
// we are not using retries yet.. or looking for errors here. We should log errors too.
requestStream.Write(messageBytes, 0, messageBytes.Length);
}
// Sends the notification and gets the response.
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.NotFound)
{
return;
}
if (response.StatusCode != HttpStatusCode.OK)
{
tries++;
if (tries > this.numOfRetries)
{
if (this.NotificationFailed != null)
{
this.NotificationFailed(this, new NotificationEventArgs(new NotificationException(string.Format(CultureInfo.InvariantCulture, "{0} notification failures for {1} for DeviceId:{2} of DeviceType:{3}. Giving up", this.numOfRetries, C2dmUrlStr,ddm.DeviceId,ddm.DeviceType))));
}
return;
}
}
else
{
sent = true;
}
}
catch (WebException e)
{
// check why we got the exception
string responseString = "";
if (e.Response != null)
{
StreamReader reader = new StreamReader(e.Response.GetResponseStream());
responseString= reader.ReadToEnd();
}
if (this.NotificationFailed != null)
{
this.NotificationFailed(this, new NotificationEventArgs(new NotificationException("Notification failed with exception " + e.Message + "for " + "https://android.googleapis.com/gcm/send"+" DeviceId: "+ddm.DeviceId+" Device Type: "+ddm.DeviceType+"Response String : "+ responseString, e)));
}
throw;
}
}
}
I once faced a similar problem too and oddly enough it was because I was not closing the response stream after reading from it. Try calling
response.Close();after you’ve done reading with theresponse. I hope it works out for you too.