1

I am using Asp.net MVC 4 Web API as a third party server to Push Notification for Android Device using GCM. It's working fine notification are being generated but the message is blank. I have spent whole day on it but could not find any solution please help me

Web API Function to Send Push Notification is as follows:

    public Notification PushToAndroidDevice(string registrationid,string message)
    {                  
        Notification notification = new Notification();
        try
        {
            var applicationID = "MY_APPLICATION_ID";    

            var SENDER_ID = "MY_SENDER_ID";

            WebRequest tRequest;
            tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
            tRequest.Method = "post";

            tRequest.ContentType = "application/x-www-form-urlencoded";
            tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));

            tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));

            string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + message + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + registrationid + "";

            Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            tRequest.ContentLength = byteArray.Length;

            Stream dataStream = tRequest.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            WebResponse tResponse = tRequest.GetResponse();

            dataStream = tResponse.GetResponseStream();

            StreamReader tReader = new StreamReader(dataStream);

            String sResponseFromServer = tReader.ReadToEnd();

            notification.Message = sResponseFromServer;
            tReader.Close();
            dataStream.Close();
            tResponse.Close();

            notification.Status = true;
        }
        catch (Exception ex)
        {
            notification.Status = false;
            notification.Message = "ERROR DESCRIPTION : " + ex.Message;
        }
        return notification;
    }

Notification is a class having two properties Status bool and Message string

public class Notification
{
    public bool Status { get; set; }
    public string Message { get; set; }
}

By this code I'm able to send notification message on android mobile but the notification is blank please help me out ...........

3
  • Got it...... there was a problem in my android code.... Commented Dec 5, 2014 at 7:33
  • could you please help me out, I am having similar functionality to be implemented in my android application. It would be helpful if you could also embed your android code. Commented Dec 11, 2014 at 12:18
  • Use AndroidHive Push Notification using GCM androidhive.info/2012/10/… Commented Dec 11, 2014 at 12:57

1 Answer 1

1

You shuould check the MSGReceiver and MSGSerice (or corresponding) class that receives the message from GCM server. You check print and see if you are receiving null. May be the variable names used there and the one you are sending from your MVC are different. This is a very short answer. If you have further question or difficulty in undestanding then please provide relevant code and filenames.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.